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 */ 25 26#include <sys/nvpair.h> 27#ifndef _KERNEL 28#include <sys/zfs_context.h> 29#include <stdlib.h> 30#else 31#include <sys/debug.h> 32#include <sys/kmem.h> 33#include <sys/param.h> 34#include <sys/debug.h> 35#endif 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 53nvlist_t * 54fnvlist_alloc(void) 55{ 56 nvlist_t *nvl; 57 VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)); 58 return (nvl); 59} 60 61void 62fnvlist_free(nvlist_t *nvl) 63{ 64 nvlist_free(nvl); 65} 66 67size_t 68fnvlist_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 */ 79char * 80fnvlist_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*/ 89void 90fnvlist_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 99nvlist_t * 100fnvlist_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 107nvlist_t * 108fnvlist_dup(nvlist_t *nvl) 109{ 110 nvlist_t *rv; 111 VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP)); 112 return (rv); 113} 114 115void 116fnvlist_merge(nvlist_t *dst, nvlist_t *src) 117{ 118 VERIFY0(nvlist_merge(dst, src, KM_SLEEP)); 119} 120 121size_t 122fnvlist_num_pairs(nvlist_t *nvl) 123{ 124 size_t count = 0; 125 nvpair_t *pair; 126 127 for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL; 128 pair = nvlist_next_nvpair(nvl, pair)) 129 count++; 130 return (count); 131} 132 133void 134fnvlist_add_boolean(nvlist_t *nvl, const char *name) 135{ 136 VERIFY0(nvlist_add_boolean(nvl, name)); 137} 138 139void 140fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val) 141{ 142 VERIFY0(nvlist_add_boolean_value(nvl, name, val)); 143} 144 145void 146fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val) 147{ 148 VERIFY0(nvlist_add_byte(nvl, name, val)); 149} 150 151void 152fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val) 153{ 154 VERIFY0(nvlist_add_int8(nvl, name, val)); 155} 156 157void 158fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val) 159{ 160 VERIFY0(nvlist_add_uint8(nvl, name, val)); 161} 162 163void 164fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val) 165{ 166 VERIFY0(nvlist_add_int16(nvl, name, val)); 167} 168 169void 170fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val) 171{ 172 VERIFY0(nvlist_add_uint16(nvl, name, val)); 173} 174 175void 176fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val) 177{ 178 VERIFY0(nvlist_add_int32(nvl, name, val)); 179} 180 181void 182fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val) 183{ 184 VERIFY0(nvlist_add_uint32(nvl, name, val)); 185} 186 187void 188fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val) 189{ 190 VERIFY0(nvlist_add_int64(nvl, name, val)); 191} 192 193void 194fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val) 195{ 196 VERIFY0(nvlist_add_uint64(nvl, name, val)); 197} 198 199void 200fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val) 201{ 202 VERIFY0(nvlist_add_string(nvl, name, val)); 203} 204 205void 206fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val) 207{ 208 VERIFY0(nvlist_add_nvlist(nvl, name, val)); 209} 210 211void 212fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair) 213{ 214 VERIFY0(nvlist_add_nvpair(nvl, pair)); 215} 216 217void 218fnvlist_add_boolean_array(nvlist_t *nvl, const char *name, 219 boolean_t *val, uint_t n) 220{ 221 VERIFY0(nvlist_add_boolean_array(nvl, name, val, n)); 222} 223 224void 225fnvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *val, uint_t n) 226{ 227 VERIFY0(nvlist_add_byte_array(nvl, name, val, n)); 228} 229 230void 231fnvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *val, uint_t n) 232{ 233 VERIFY0(nvlist_add_int8_array(nvl, name, val, n)); 234} 235 236void 237fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *val, uint_t n) 238{ 239 VERIFY0(nvlist_add_uint8_array(nvl, name, val, n)); 240} 241 242void 243fnvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *val, uint_t n) 244{ 245 VERIFY0(nvlist_add_int16_array(nvl, name, val, n)); 246} 247 248void 249fnvlist_add_uint16_array(nvlist_t *nvl, const char *name, 250 uint16_t *val, uint_t n) 251{ 252 VERIFY0(nvlist_add_uint16_array(nvl, name, val, n)); 253} 254 255void 256fnvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *val, uint_t n) 257{ 258 VERIFY0(nvlist_add_int32_array(nvl, name, val, n)); 259} 260 261void 262fnvlist_add_uint32_array(nvlist_t *nvl, const char *name, 263 uint32_t *val, uint_t n) 264{ 265 VERIFY0(nvlist_add_uint32_array(nvl, name, val, n)); 266} 267 268void 269fnvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *val, uint_t n) 270{ 271 VERIFY0(nvlist_add_int64_array(nvl, name, val, n)); 272} 273 274void 275fnvlist_add_uint64_array(nvlist_t *nvl, const char *name, 276 uint64_t *val, uint_t n) 277{ 278 VERIFY0(nvlist_add_uint64_array(nvl, name, val, n)); 279} 280 281void 282fnvlist_add_string_array(nvlist_t *nvl, const char *name, 283 char * const *val, uint_t n) 284{ 285 VERIFY0(nvlist_add_string_array(nvl, name, val, n)); 286} 287 288void 289fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name, 290 nvlist_t **val, uint_t n) 291{ 292 VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n)); 293} 294 295void 296fnvlist_remove(nvlist_t *nvl, const char *name) 297{ 298 VERIFY0(nvlist_remove_all(nvl, name)); 299} 300 301void 302fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair) 303{ 304 VERIFY0(nvlist_remove_nvpair(nvl, pair)); 305} 306 307nvpair_t * 308fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name) 309{ 310 nvpair_t *rv; 311 VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv)); 312 return (rv); 313} 314 315/* returns B_TRUE if the entry exists */ 316boolean_t 317fnvlist_lookup_boolean(nvlist_t *nvl, const char *name) 318{ 319 return (nvlist_lookup_boolean(nvl, name) == 0); 320} 321 322boolean_t 323fnvlist_lookup_boolean_value(nvlist_t *nvl, const char *name) 324{ 325 boolean_t rv; 326 VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv)); 327 return (rv); 328} 329 330uchar_t 331fnvlist_lookup_byte(nvlist_t *nvl, const char *name) 332{ 333 uchar_t rv; 334 VERIFY0(nvlist_lookup_byte(nvl, name, &rv)); 335 return (rv); 336} 337 338int8_t 339fnvlist_lookup_int8(nvlist_t *nvl, const char *name) 340{ 341 int8_t rv; 342 VERIFY0(nvlist_lookup_int8(nvl, name, &rv)); 343 return (rv); 344} 345 346int16_t 347fnvlist_lookup_int16(nvlist_t *nvl, const char *name) 348{ 349 int16_t rv; 350 VERIFY0(nvlist_lookup_int16(nvl, name, &rv)); 351 return (rv); 352} 353 354int32_t 355fnvlist_lookup_int32(nvlist_t *nvl, const char *name) 356{ 357 int32_t rv; 358 VERIFY0(nvlist_lookup_int32(nvl, name, &rv)); 359 return (rv); 360} 361 362int64_t 363fnvlist_lookup_int64(nvlist_t *nvl, const char *name) 364{ 365 int64_t rv; 366 VERIFY0(nvlist_lookup_int64(nvl, name, &rv)); 367 return (rv); 368} 369 370uint8_t 371fnvlist_lookup_uint8_t(nvlist_t *nvl, const char *name) 372{ 373 uint8_t rv; 374 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv)); 375 return (rv); 376} 377 378uint16_t 379fnvlist_lookup_uint16(nvlist_t *nvl, const char *name) 380{ 381 uint16_t rv; 382 VERIFY0(nvlist_lookup_uint16(nvl, name, &rv)); 383 return (rv); 384} 385 386uint32_t 387fnvlist_lookup_uint32(nvlist_t *nvl, const char *name) 388{ 389 uint32_t rv; 390 VERIFY0(nvlist_lookup_uint32(nvl, name, &rv)); 391 return (rv); 392} 393 394uint64_t 395fnvlist_lookup_uint64(nvlist_t *nvl, const char *name) 396{ 397 uint64_t rv; 398 VERIFY0(nvlist_lookup_uint64(nvl, name, &rv)); 399 return (rv); 400} 401 402char * 403fnvlist_lookup_string(nvlist_t *nvl, const char *name) 404{ 405 char *rv; 406 VERIFY0(nvlist_lookup_string(nvl, name, &rv)); 407 return (rv); 408} 409 410nvlist_t * 411fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name) 412{ 413 nvlist_t *rv; 414 VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv)); 415 return (rv); 416} 417 418boolean_t 419fnvpair_value_boolean_value(nvpair_t *nvp) 420{ 421 boolean_t rv; 422 VERIFY0(nvpair_value_boolean_value(nvp, &rv)); 423 return (rv); 424} 425 426uchar_t 427fnvpair_value_byte(nvpair_t *nvp) 428{ 429 uchar_t rv; 430 VERIFY0(nvpair_value_byte(nvp, &rv)); 431 return (rv); 432} 433 434int8_t 435fnvpair_value_int8(nvpair_t *nvp) 436{ 437 int8_t rv; 438 VERIFY0(nvpair_value_int8(nvp, &rv)); 439 return (rv); 440} 441 442int16_t 443fnvpair_value_int16(nvpair_t *nvp) 444{ 445 int16_t rv; 446 VERIFY0(nvpair_value_int16(nvp, &rv)); 447 return (rv); 448} 449 450int32_t 451fnvpair_value_int32(nvpair_t *nvp) 452{ 453 int32_t rv; 454 VERIFY0(nvpair_value_int32(nvp, &rv)); 455 return (rv); 456} 457 458int64_t 459fnvpair_value_int64(nvpair_t *nvp) 460{ 461 int64_t rv; 462 VERIFY0(nvpair_value_int64(nvp, &rv)); 463 return (rv); 464} 465 466uint8_t 467fnvpair_value_uint8_t(nvpair_t *nvp) 468{ 469 uint8_t rv; 470 VERIFY0(nvpair_value_uint8(nvp, &rv)); 471 return (rv); 472} 473 474uint16_t 475fnvpair_value_uint16(nvpair_t *nvp) 476{ 477 uint16_t rv; 478 VERIFY0(nvpair_value_uint16(nvp, &rv)); 479 return (rv); 480} 481 482uint32_t 483fnvpair_value_uint32(nvpair_t *nvp) 484{ 485 uint32_t rv; 486 VERIFY0(nvpair_value_uint32(nvp, &rv)); 487 return (rv); 488} 489 490uint64_t 491fnvpair_value_uint64(nvpair_t *nvp) 492{ 493 uint64_t rv; 494 VERIFY0(nvpair_value_uint64(nvp, &rv)); 495 return (rv); 496} 497 498char * 499fnvpair_value_string(nvpair_t *nvp) 500{ 501 char *rv; 502 VERIFY0(nvpair_value_string(nvp, &rv)); 503 return (rv); 504} 505 506nvlist_t * 507fnvpair_value_nvlist(nvpair_t *nvp) 508{ 509 nvlist_t *rv; 510 VERIFY0(nvpair_value_nvlist(nvp, &rv)); 511 return (rv); 512} 513