1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 24 */ 25 26 #include <sys/nvpair.h> 27 #include <sys/kmem.h> 28 #include <sys/debug.h> 29 #include <sys/param.h> 30 #ifndef _KERNEL 31 #include <stdlib.h> 32 #endif 33 34 /* 35 * "Force" nvlist wrapper. 36 * 37 * These functions wrap the nvlist_* functions with assertions that assume 38 * the operation is successful. This allows the caller's code to be much 39 * more readable, especially for the fnvlist_lookup_* and fnvpair_value_* 40 * functions, which can return the requested value (rather than filling in 41 * a pointer). 42 * 43 * These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate 44 * with KM_SLEEP. 45 * 46 * More wrappers should be added as needed -- for example 47 * nvlist_lookup_*_array and nvpair_value_*_array. 48 */ 49 50 nvlist_t * 51 fnvlist_alloc(void) 52 { 53 nvlist_t *nvl; 54 VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)); 55 return (nvl); 56 } 57 58 void 59 fnvlist_free(nvlist_t *nvl) 60 { 61 nvlist_free(nvl); 62 } 63 64 size_t 65 fnvlist_size(nvlist_t *nvl) 66 { 67 size_t size; 68 VERIFY0(nvlist_size(nvl, &size, NV_ENCODE_NATIVE)); 69 return (size); 70 } 71 72 /* 73 * Returns allocated buffer of size *sizep. Caller must free the buffer with 74 * fnvlist_pack_free(). 75 */ 76 char * 77 fnvlist_pack(nvlist_t *nvl, size_t *sizep) 78 { 79 char *packed = 0; 80 VERIFY3U(nvlist_pack(nvl, &packed, sizep, NV_ENCODE_NATIVE, 81 KM_SLEEP), ==, 0); 82 return (packed); 83 } 84 85 void 86 fnvlist_pack_free(char *pack, size_t size) 87 { 88 #ifdef _KERNEL 89 kmem_free(pack, size); 90 #else 91 (void) size; 92 free(pack); 93 #endif 94 } 95 96 nvlist_t * 97 fnvlist_unpack(char *buf, size_t buflen) 98 { 99 nvlist_t *rv; 100 VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP)); 101 return (rv); 102 } 103 104 nvlist_t * 105 fnvlist_dup(const nvlist_t *nvl) 106 { 107 nvlist_t *rv; 108 VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP)); 109 return (rv); 110 } 111 112 void 113 fnvlist_merge(nvlist_t *dst, nvlist_t *src) 114 { 115 VERIFY0(nvlist_merge(dst, src, KM_SLEEP)); 116 } 117 118 size_t 119 fnvlist_num_pairs(nvlist_t *nvl) 120 { 121 size_t count = 0; 122 nvpair_t *pair; 123 124 for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL; 125 pair = nvlist_next_nvpair(nvl, pair)) 126 count++; 127 return (count); 128 } 129 130 void 131 fnvlist_add_boolean(nvlist_t *nvl, const char *name) 132 { 133 VERIFY0(nvlist_add_boolean(nvl, name)); 134 } 135 136 void 137 fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val) 138 { 139 VERIFY0(nvlist_add_boolean_value(nvl, name, val)); 140 } 141 142 void 143 fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val) 144 { 145 VERIFY0(nvlist_add_byte(nvl, name, val)); 146 } 147 148 void 149 fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val) 150 { 151 VERIFY0(nvlist_add_int8(nvl, name, val)); 152 } 153 154 void 155 fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val) 156 { 157 VERIFY0(nvlist_add_uint8(nvl, name, val)); 158 } 159 160 void 161 fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val) 162 { 163 VERIFY0(nvlist_add_int16(nvl, name, val)); 164 } 165 166 void 167 fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val) 168 { 169 VERIFY0(nvlist_add_uint16(nvl, name, val)); 170 } 171 172 void 173 fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val) 174 { 175 VERIFY0(nvlist_add_int32(nvl, name, val)); 176 } 177 178 void 179 fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val) 180 { 181 VERIFY0(nvlist_add_uint32(nvl, name, val)); 182 } 183 184 void 185 fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val) 186 { 187 VERIFY0(nvlist_add_int64(nvl, name, val)); 188 } 189 190 void 191 fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val) 192 { 193 VERIFY0(nvlist_add_uint64(nvl, name, val)); 194 } 195 196 void 197 fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val) 198 { 199 VERIFY0(nvlist_add_string(nvl, name, val)); 200 } 201 202 void 203 fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val) 204 { 205 VERIFY0(nvlist_add_nvlist(nvl, name, val)); 206 } 207 208 void 209 fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair) 210 { 211 VERIFY0(nvlist_add_nvpair(nvl, pair)); 212 } 213 214 void 215 fnvlist_add_boolean_array(nvlist_t *nvl, const char *name, 216 const boolean_t *val, uint_t n) 217 { 218 VERIFY0(nvlist_add_boolean_array(nvl, name, val, n)); 219 } 220 221 void 222 fnvlist_add_byte_array(nvlist_t *nvl, const char *name, const uchar_t *val, 223 uint_t n) 224 { 225 VERIFY0(nvlist_add_byte_array(nvl, name, val, n)); 226 } 227 228 void 229 fnvlist_add_int8_array(nvlist_t *nvl, const char *name, const int8_t *val, 230 uint_t n) 231 { 232 VERIFY0(nvlist_add_int8_array(nvl, name, val, n)); 233 } 234 235 void 236 fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, const uint8_t *val, 237 uint_t n) 238 { 239 VERIFY0(nvlist_add_uint8_array(nvl, name, val, n)); 240 } 241 242 void 243 fnvlist_add_int16_array(nvlist_t *nvl, const char *name, const int16_t *val, 244 uint_t n) 245 { 246 VERIFY0(nvlist_add_int16_array(nvl, name, val, n)); 247 } 248 249 void 250 fnvlist_add_uint16_array(nvlist_t *nvl, const char *name, 251 const uint16_t *val, uint_t n) 252 { 253 VERIFY0(nvlist_add_uint16_array(nvl, name, val, n)); 254 } 255 256 void 257 fnvlist_add_int32_array(nvlist_t *nvl, const char *name, const int32_t *val, 258 uint_t n) 259 { 260 VERIFY0(nvlist_add_int32_array(nvl, name, val, n)); 261 } 262 263 void 264 fnvlist_add_uint32_array(nvlist_t *nvl, const char *name, 265 const uint32_t *val, uint_t n) 266 { 267 VERIFY0(nvlist_add_uint32_array(nvl, name, val, n)); 268 } 269 270 void 271 fnvlist_add_int64_array(nvlist_t *nvl, const char *name, const int64_t *val, 272 uint_t n) 273 { 274 VERIFY0(nvlist_add_int64_array(nvl, name, val, n)); 275 } 276 277 void 278 fnvlist_add_uint64_array(nvlist_t *nvl, const char *name, 279 const uint64_t *val, uint_t n) 280 { 281 VERIFY0(nvlist_add_uint64_array(nvl, name, val, n)); 282 } 283 284 void 285 fnvlist_add_string_array(nvlist_t *nvl, const char *name, 286 const char * const *val, uint_t n) 287 { 288 VERIFY0(nvlist_add_string_array(nvl, name, val, n)); 289 } 290 291 void 292 fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name, 293 const nvlist_t * const *val, uint_t n) 294 { 295 VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n)); 296 } 297 298 void 299 fnvlist_remove(nvlist_t *nvl, const char *name) 300 { 301 VERIFY0(nvlist_remove_all(nvl, name)); 302 } 303 304 void 305 fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair) 306 { 307 VERIFY0(nvlist_remove_nvpair(nvl, pair)); 308 } 309 310 nvpair_t * 311 fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name) 312 { 313 nvpair_t *rv; 314 VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv)); 315 return (rv); 316 } 317 318 /* returns B_TRUE if the entry exists */ 319 boolean_t 320 fnvlist_lookup_boolean(const nvlist_t *nvl, const char *name) 321 { 322 return (nvlist_lookup_boolean(nvl, name) == 0); 323 } 324 325 boolean_t 326 fnvlist_lookup_boolean_value(const nvlist_t *nvl, const char *name) 327 { 328 boolean_t rv; 329 VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv)); 330 return (rv); 331 } 332 333 uchar_t 334 fnvlist_lookup_byte(const nvlist_t *nvl, const char *name) 335 { 336 uchar_t rv; 337 VERIFY0(nvlist_lookup_byte(nvl, name, &rv)); 338 return (rv); 339 } 340 341 int8_t 342 fnvlist_lookup_int8(const nvlist_t *nvl, const char *name) 343 { 344 int8_t rv; 345 VERIFY0(nvlist_lookup_int8(nvl, name, &rv)); 346 return (rv); 347 } 348 349 int16_t 350 fnvlist_lookup_int16(const nvlist_t *nvl, const char *name) 351 { 352 int16_t rv; 353 VERIFY0(nvlist_lookup_int16(nvl, name, &rv)); 354 return (rv); 355 } 356 357 int32_t 358 fnvlist_lookup_int32(const nvlist_t *nvl, const char *name) 359 { 360 int32_t rv; 361 VERIFY0(nvlist_lookup_int32(nvl, name, &rv)); 362 return (rv); 363 } 364 365 int64_t 366 fnvlist_lookup_int64(const nvlist_t *nvl, const char *name) 367 { 368 int64_t rv; 369 VERIFY0(nvlist_lookup_int64(nvl, name, &rv)); 370 return (rv); 371 } 372 373 uint8_t 374 fnvlist_lookup_uint8(const nvlist_t *nvl, const char *name) 375 { 376 uint8_t rv; 377 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv)); 378 return (rv); 379 } 380 381 uint16_t 382 fnvlist_lookup_uint16(const nvlist_t *nvl, const char *name) 383 { 384 uint16_t rv; 385 VERIFY0(nvlist_lookup_uint16(nvl, name, &rv)); 386 return (rv); 387 } 388 389 uint32_t 390 fnvlist_lookup_uint32(const nvlist_t *nvl, const char *name) 391 { 392 uint32_t rv; 393 VERIFY0(nvlist_lookup_uint32(nvl, name, &rv)); 394 return (rv); 395 } 396 397 uint64_t 398 fnvlist_lookup_uint64(const nvlist_t *nvl, const char *name) 399 { 400 uint64_t rv; 401 VERIFY0(nvlist_lookup_uint64(nvl, name, &rv)); 402 return (rv); 403 } 404 405 const char * 406 fnvlist_lookup_string(const nvlist_t *nvl, const char *name) 407 { 408 const char *rv; 409 VERIFY0(nvlist_lookup_string(nvl, name, &rv)); 410 return (rv); 411 } 412 413 nvlist_t * 414 fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name) 415 { 416 nvlist_t *rv; 417 VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv)); 418 return (rv); 419 } 420 boolean_t * 421 fnvlist_lookup_boolean_array(nvlist_t *nvl, const char *name, uint_t *n) 422 { 423 boolean_t *rv; 424 VERIFY0(nvlist_lookup_boolean_array(nvl, name, &rv, n)); 425 return (rv); 426 } 427 428 uchar_t * 429 fnvlist_lookup_byte_array(nvlist_t *nvl, const char *name, uint_t *n) 430 { 431 uchar_t *rv; 432 VERIFY0(nvlist_lookup_byte_array(nvl, name, &rv, n)); 433 return (rv); 434 } 435 436 int8_t * 437 fnvlist_lookup_int8_array(nvlist_t *nvl, const char *name, uint_t *n) 438 { 439 int8_t *rv; 440 VERIFY0(nvlist_lookup_int8_array(nvl, name, &rv, n)); 441 return (rv); 442 } 443 444 uint8_t * 445 fnvlist_lookup_uint8_array(nvlist_t *nvl, const char *name, uint_t *n) 446 { 447 uint8_t *rv; 448 VERIFY0(nvlist_lookup_uint8_array(nvl, name, &rv, n)); 449 return (rv); 450 } 451 452 int16_t * 453 fnvlist_lookup_int16_array(nvlist_t *nvl, const char *name, uint_t *n) 454 { 455 int16_t *rv; 456 VERIFY0(nvlist_lookup_int16_array(nvl, name, &rv, n)); 457 return (rv); 458 } 459 460 uint16_t * 461 fnvlist_lookup_uint16_array(nvlist_t *nvl, const char *name, uint_t *n) 462 { 463 uint16_t *rv; 464 VERIFY0(nvlist_lookup_uint16_array(nvl, name, &rv, n)); 465 return (rv); 466 } 467 468 int32_t * 469 fnvlist_lookup_int32_array(nvlist_t *nvl, const char *name, uint_t *n) 470 { 471 int32_t *rv; 472 VERIFY0(nvlist_lookup_int32_array(nvl, name, &rv, n)); 473 return (rv); 474 } 475 476 uint32_t * 477 fnvlist_lookup_uint32_array(nvlist_t *nvl, const char *name, uint_t *n) 478 { 479 uint32_t *rv; 480 VERIFY0(nvlist_lookup_uint32_array(nvl, name, &rv, n)); 481 return (rv); 482 } 483 484 int64_t * 485 fnvlist_lookup_int64_array(nvlist_t *nvl, const char *name, uint_t *n) 486 { 487 int64_t *rv; 488 VERIFY0(nvlist_lookup_int64_array(nvl, name, &rv, n)); 489 return (rv); 490 } 491 492 uint64_t * 493 fnvlist_lookup_uint64_array(nvlist_t *nvl, const char *name, uint_t *n) 494 { 495 uint64_t *rv; 496 VERIFY0(nvlist_lookup_uint64_array(nvl, name, &rv, n)); 497 return (rv); 498 } 499 500 boolean_t 501 fnvpair_value_boolean_value(const nvpair_t *nvp) 502 { 503 boolean_t rv; 504 VERIFY0(nvpair_value_boolean_value(nvp, &rv)); 505 return (rv); 506 } 507 508 uchar_t 509 fnvpair_value_byte(const nvpair_t *nvp) 510 { 511 uchar_t rv; 512 VERIFY0(nvpair_value_byte(nvp, &rv)); 513 return (rv); 514 } 515 516 int8_t 517 fnvpair_value_int8(const nvpair_t *nvp) 518 { 519 int8_t rv; 520 VERIFY0(nvpair_value_int8(nvp, &rv)); 521 return (rv); 522 } 523 524 int16_t 525 fnvpair_value_int16(const nvpair_t *nvp) 526 { 527 int16_t rv; 528 VERIFY0(nvpair_value_int16(nvp, &rv)); 529 return (rv); 530 } 531 532 int32_t 533 fnvpair_value_int32(const nvpair_t *nvp) 534 { 535 int32_t rv; 536 VERIFY0(nvpair_value_int32(nvp, &rv)); 537 return (rv); 538 } 539 540 int64_t 541 fnvpair_value_int64(const nvpair_t *nvp) 542 { 543 int64_t rv; 544 VERIFY0(nvpair_value_int64(nvp, &rv)); 545 return (rv); 546 } 547 548 uint8_t 549 fnvpair_value_uint8(const nvpair_t *nvp) 550 { 551 uint8_t rv; 552 VERIFY0(nvpair_value_uint8(nvp, &rv)); 553 return (rv); 554 } 555 556 uint16_t 557 fnvpair_value_uint16(const nvpair_t *nvp) 558 { 559 uint16_t rv; 560 VERIFY0(nvpair_value_uint16(nvp, &rv)); 561 return (rv); 562 } 563 564 uint32_t 565 fnvpair_value_uint32(const nvpair_t *nvp) 566 { 567 uint32_t rv; 568 VERIFY0(nvpair_value_uint32(nvp, &rv)); 569 return (rv); 570 } 571 572 uint64_t 573 fnvpair_value_uint64(const nvpair_t *nvp) 574 { 575 uint64_t rv; 576 VERIFY0(nvpair_value_uint64(nvp, &rv)); 577 return (rv); 578 } 579 580 const char * 581 fnvpair_value_string(const nvpair_t *nvp) 582 { 583 const char *rv; 584 VERIFY0(nvpair_value_string(nvp, &rv)); 585 return (rv); 586 } 587 588 nvlist_t * 589 fnvpair_value_nvlist(nvpair_t *nvp) 590 { 591 nvlist_t *rv; 592 VERIFY0(nvpair_value_nvlist(nvp, &rv)); 593 return (rv); 594 } 595 596 #if defined(_KERNEL) 597 598 EXPORT_SYMBOL(fnvlist_alloc); 599 EXPORT_SYMBOL(fnvlist_free); 600 EXPORT_SYMBOL(fnvlist_size); 601 EXPORT_SYMBOL(fnvlist_pack); 602 EXPORT_SYMBOL(fnvlist_pack_free); 603 EXPORT_SYMBOL(fnvlist_unpack); 604 EXPORT_SYMBOL(fnvlist_dup); 605 EXPORT_SYMBOL(fnvlist_merge); 606 607 EXPORT_SYMBOL(fnvlist_add_nvpair); 608 EXPORT_SYMBOL(fnvlist_add_boolean); 609 EXPORT_SYMBOL(fnvlist_add_boolean_value); 610 EXPORT_SYMBOL(fnvlist_add_byte); 611 EXPORT_SYMBOL(fnvlist_add_int8); 612 EXPORT_SYMBOL(fnvlist_add_uint8); 613 EXPORT_SYMBOL(fnvlist_add_int16); 614 EXPORT_SYMBOL(fnvlist_add_uint16); 615 EXPORT_SYMBOL(fnvlist_add_int32); 616 EXPORT_SYMBOL(fnvlist_add_uint32); 617 EXPORT_SYMBOL(fnvlist_add_int64); 618 EXPORT_SYMBOL(fnvlist_add_uint64); 619 EXPORT_SYMBOL(fnvlist_add_string); 620 EXPORT_SYMBOL(fnvlist_add_nvlist); 621 EXPORT_SYMBOL(fnvlist_add_boolean_array); 622 EXPORT_SYMBOL(fnvlist_add_byte_array); 623 EXPORT_SYMBOL(fnvlist_add_int8_array); 624 EXPORT_SYMBOL(fnvlist_add_uint8_array); 625 EXPORT_SYMBOL(fnvlist_add_int16_array); 626 EXPORT_SYMBOL(fnvlist_add_uint16_array); 627 EXPORT_SYMBOL(fnvlist_add_int32_array); 628 EXPORT_SYMBOL(fnvlist_add_uint32_array); 629 EXPORT_SYMBOL(fnvlist_add_int64_array); 630 EXPORT_SYMBOL(fnvlist_add_uint64_array); 631 EXPORT_SYMBOL(fnvlist_add_string_array); 632 EXPORT_SYMBOL(fnvlist_add_nvlist_array); 633 634 EXPORT_SYMBOL(fnvlist_remove); 635 EXPORT_SYMBOL(fnvlist_remove_nvpair); 636 637 EXPORT_SYMBOL(fnvlist_lookup_nvpair); 638 EXPORT_SYMBOL(fnvlist_lookup_boolean); 639 EXPORT_SYMBOL(fnvlist_lookup_boolean_value); 640 EXPORT_SYMBOL(fnvlist_lookup_byte); 641 EXPORT_SYMBOL(fnvlist_lookup_int8); 642 EXPORT_SYMBOL(fnvlist_lookup_uint8); 643 EXPORT_SYMBOL(fnvlist_lookup_int16); 644 EXPORT_SYMBOL(fnvlist_lookup_uint16); 645 EXPORT_SYMBOL(fnvlist_lookup_int32); 646 EXPORT_SYMBOL(fnvlist_lookup_uint32); 647 EXPORT_SYMBOL(fnvlist_lookup_int64); 648 EXPORT_SYMBOL(fnvlist_lookup_uint64); 649 EXPORT_SYMBOL(fnvlist_lookup_string); 650 EXPORT_SYMBOL(fnvlist_lookup_nvlist); 651 652 EXPORT_SYMBOL(fnvpair_value_boolean_value); 653 EXPORT_SYMBOL(fnvpair_value_byte); 654 EXPORT_SYMBOL(fnvpair_value_int8); 655 EXPORT_SYMBOL(fnvpair_value_uint8); 656 EXPORT_SYMBOL(fnvpair_value_int16); 657 EXPORT_SYMBOL(fnvpair_value_uint16); 658 EXPORT_SYMBOL(fnvpair_value_int32); 659 EXPORT_SYMBOL(fnvpair_value_uint32); 660 EXPORT_SYMBOL(fnvpair_value_int64); 661 EXPORT_SYMBOL(fnvpair_value_uint64); 662 EXPORT_SYMBOL(fnvpair_value_string); 663 EXPORT_SYMBOL(fnvpair_value_nvlist); 664 EXPORT_SYMBOL(fnvlist_num_pairs); 665 666 #endif 667