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