213 case EZFS_UNKNOWN: 214 return (dgettext(TEXT_DOMAIN, "unknown error")); 215 default: 216 assert(hdl->libzfs_error == 0); 217 return (dgettext(TEXT_DOMAIN, "no error")); 218 } 219} 220 221/*PRINTFLIKE2*/ 222void 223zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 224{ 225 va_list ap; 226 227 va_start(ap, fmt); 228 229 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 230 fmt, ap); 231 hdl->libzfs_desc_active = 1; 232 233 va_end(ap); 234} 235 236static void 237zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 238{ 239 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 240 fmt, ap); 241 hdl->libzfs_error = error; 242 243 if (hdl->libzfs_desc_active) 244 hdl->libzfs_desc_active = 0; 245 else 246 hdl->libzfs_desc[0] = '\0'; 247 248 if (hdl->libzfs_printerr) { 249 if (error == EZFS_UNKNOWN) { 250 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 251 "error: %s\n"), libzfs_error_description(hdl)); 252 abort(); 253 } 254 255 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 256 libzfs_error_description(hdl)); 257 if (error == EZFS_NOMEM) 258 exit(1); 259 } 260} 261 262int 263zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 264{ 265 return (zfs_error_fmt(hdl, error, "%s", msg)); 266} 267 268/*PRINTFLIKE3*/ 269int 270zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 271{ 272 va_list ap; 273 274 va_start(ap, fmt); 275 276 zfs_verror(hdl, error, fmt, ap); 277 278 va_end(ap); 279 280 return (-1); 281} 282 283static int 284zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 285 va_list ap) 286{ 287 switch (error) { 288 case EPERM: 289 case EACCES: 290 zfs_verror(hdl, EZFS_PERM, fmt, ap); 291 return (-1); 292 293 case ECANCELED: 294 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 295 return (-1); 296 297 case EIO: 298 zfs_verror(hdl, EZFS_IO, fmt, ap); 299 return (-1); 300 301 case EINTR: 302 zfs_verror(hdl, EZFS_INTR, fmt, ap); 303 return (-1); 304 } 305 306 return (0); 307} 308 309int 310zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 311{ 312 return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 313} 314 315/*PRINTFLIKE3*/ 316int 317zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 318{ 319 va_list ap; 320 321 va_start(ap, fmt); 322 323 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 324 va_end(ap); 325 return (-1); 326 } 327 328 switch (error) { 329 case ENXIO: 330 case ENODEV: 331 zfs_verror(hdl, EZFS_IO, fmt, ap); 332 break; 333 334 case ENOENT: 335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 336 "dataset does not exist")); 337 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 338 break; 339 340 case ENOSPC: 341 case EDQUOT: 342 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 343 return (-1); 344 345 case EEXIST: 346 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 347 "dataset already exists")); 348 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 349 break; 350 351 case EBUSY: 352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 353 "dataset is busy")); 354 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 355 break; 356 case EROFS: 357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 358 "snapshot permissions cannot be modified")); 359 zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 360 break; 361 case ENAMETOOLONG: 362 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 363 break; 364 case ENOTSUP: 365 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 366 break; 367 case EAGAIN: 368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 369 "pool I/O is currently suspended")); 370 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 371 break; 372 default: 373 zfs_error_aux(hdl, strerror(errno)); 374 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 375 break; 376 } 377 378 va_end(ap); 379 return (-1); 380} 381 382int 383zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 384{ 385 return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 386} 387 388/*PRINTFLIKE3*/ 389int 390zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 391{ 392 va_list ap; 393 394 va_start(ap, fmt); 395 396 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 397 va_end(ap); 398 return (-1); 399 } 400 401 switch (error) { 402 case ENODEV: 403 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 404 break; 405 406 case ENOENT: 407 zfs_error_aux(hdl, 408 dgettext(TEXT_DOMAIN, "no such pool or dataset")); 409 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 410 break; 411 412 case EEXIST: 413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 414 "pool already exists")); 415 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 416 break; 417 418 case EBUSY: 419 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 420 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 421 break; 422 423 case ENXIO: 424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 425 "one or more devices is currently unavailable")); 426 zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 427 break; 428 429 case ENAMETOOLONG: 430 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 431 break; 432 433 case ENOTSUP: 434 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 435 break; 436 437 case EINVAL: 438 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 439 break; 440 441 case ENOSPC: 442 case EDQUOT: 443 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 444 return (-1); 445 case EAGAIN: 446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 447 "pool I/O is currently suspended")); 448 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 449 break; 450 451 default: 452 zfs_error_aux(hdl, strerror(error)); 453 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 454 } 455 456 va_end(ap); 457 return (-1); 458} 459 460/* 461 * Display an out of memory error message and abort the current program. 462 */ 463int 464no_memory(libzfs_handle_t *hdl) 465{ 466 return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 467} 468 469/* 470 * A safe form of malloc() which will die if the allocation fails. 471 */ 472void * 473zfs_alloc(libzfs_handle_t *hdl, size_t size) 474{ 475 void *data; 476 477 if ((data = calloc(1, size)) == NULL) 478 (void) no_memory(hdl); 479 480 return (data); 481} 482 483/* 484 * A safe form of realloc(), which also zeroes newly allocated space. 485 */ 486void * 487zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 488{ 489 void *ret; 490 491 if ((ret = realloc(ptr, newsize)) == NULL) { 492 (void) no_memory(hdl); 493 return (NULL); 494 } 495 496 bzero((char *)ret + oldsize, (newsize - oldsize)); 497 return (ret); 498} 499 500/* 501 * A safe form of strdup() which will die if the allocation fails. 502 */ 503char * 504zfs_strdup(libzfs_handle_t *hdl, const char *str) 505{ 506 char *ret; 507 508 if ((ret = strdup(str)) == NULL) 509 (void) no_memory(hdl); 510 511 return (ret); 512} 513 514/* 515 * Convert a number to an appropriately human-readable output. 516 */ 517void 518zfs_nicenum(uint64_t num, char *buf, size_t buflen) 519{ 520 uint64_t n = num; 521 int index = 0; 522 char u; 523 524 while (n >= 1024) { 525 n /= 1024; 526 index++; 527 } 528 529 u = " KMGTPE"[index]; 530 531 if (index == 0) { 532 (void) snprintf(buf, buflen, "%llu", n); 533 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 534 /* 535 * If this is an even multiple of the base, always display 536 * without any decimal precision. 537 */ 538 (void) snprintf(buf, buflen, "%llu%c", n, u); 539 } else { 540 /* 541 * We want to choose a precision that reflects the best choice 542 * for fitting in 5 characters. This can get rather tricky when 543 * we have numbers that are very close to an order of magnitude. 544 * For example, when displaying 10239 (which is really 9.999K), 545 * we want only a single place of precision for 10.0K. We could 546 * develop some complex heuristics for this, but it's much 547 * easier just to try each combination in turn. 548 */ 549 int i; 550 for (i = 2; i >= 0; i--) { 551 if (snprintf(buf, buflen, "%.*f%c", i, 552 (double)num / (1ULL << 10 * index), u) <= 5) 553 break; 554 } 555 } 556} 557 558void 559libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 560{ 561 hdl->libzfs_printerr = printerr; 562} 563 564static int 565libzfs_load(void) 566{ 567 int error; 568 569 if (modfind("zfs") < 0) { 570 /* Not present in kernel, try loading it. */ 571 if (kldload("zfs") < 0 || modfind("zfs") < 0) { 572 if (errno != EEXIST) 573 return (error); 574 } 575 } 576 return (0); 577} 578 579libzfs_handle_t * 580libzfs_init(void) 581{ 582 libzfs_handle_t *hdl; 583 584 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 585 return (NULL); 586 } 587 588 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 589 if (libzfs_load() == 0) 590 hdl->libzfs_fd = open(ZFS_DEV, O_RDWR); 591 if (hdl->libzfs_fd < 0) { 592 free(hdl); 593 return (NULL); 594 } 595 } 596 597 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 598 (void) close(hdl->libzfs_fd); 599 free(hdl); 600 return (NULL); 601 } 602 603 hdl->libzfs_sharetab = fopen(ZFS_EXPORTS_PATH, "r"); 604 605 zfs_prop_init(); 606 zpool_prop_init(); 607 libzfs_mnttab_init(hdl); 608 609 return (hdl); 610} 611 612void 613libzfs_fini(libzfs_handle_t *hdl) 614{ 615 (void) close(hdl->libzfs_fd); 616 if (hdl->libzfs_mnttab) 617 (void) fclose(hdl->libzfs_mnttab); 618 if (hdl->libzfs_sharetab) 619 (void) fclose(hdl->libzfs_sharetab); 620 zfs_uninit_libshare(hdl); 621 if (hdl->libzfs_log_str) 622 (void) free(hdl->libzfs_log_str); 623 zpool_free_handles(hdl); 624 namespace_clear(hdl); 625 libzfs_mnttab_fini(hdl); 626 free(hdl); 627} 628 629libzfs_handle_t * 630zpool_get_handle(zpool_handle_t *zhp) 631{ 632 return (zhp->zpool_hdl); 633} 634 635libzfs_handle_t * 636zfs_get_handle(zfs_handle_t *zhp) 637{ 638 return (zhp->zfs_hdl); 639} 640 641zpool_handle_t * 642zfs_get_pool_handle(const zfs_handle_t *zhp) 643{ 644 return (zhp->zpool_hdl); 645} 646 647/* 648 * Given a name, determine whether or not it's a valid path 649 * (starts with '/' or "./"). If so, walk the mnttab trying 650 * to match the device number. If not, treat the path as an 651 * fs/vol/snap name. 652 */ 653zfs_handle_t * 654zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 655{ 656 struct statfs statbuf; 657 658 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 659 /* 660 * It's not a valid path, assume it's a name of type 'argtype'. 661 */ 662 return (zfs_open(hdl, path, argtype)); 663 } 664 665 if (statfs(path, &statbuf) != 0) { 666 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 667 return (NULL); 668 } 669 670 if (strcmp(statbuf.f_fstypename, MNTTYPE_ZFS) != 0) { 671 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 672 path); 673 return (NULL); 674 } 675 676 return (zfs_open(hdl, statbuf.f_mntfromname, ZFS_TYPE_FILESYSTEM)); 677} 678 679/* 680 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 681 * an ioctl(). 682 */ 683int 684zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 685{ 686 if (len == 0) 687 len = 2048; 688 zc->zc_nvlist_dst_size = len; 689 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 690 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == 0) 691 return (-1); 692 693 return (0); 694} 695 696/* 697 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 698 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 699 * filled in by the kernel to indicate the actual required size. 700 */ 701int 702zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 703{ 704 free((void *)(uintptr_t)zc->zc_nvlist_dst); 705 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 706 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 707 == 0) 708 return (-1); 709 710 return (0); 711} 712 713/* 714 * Called to free the src and dst nvlists stored in the command structure. 715 */ 716void 717zcmd_free_nvlists(zfs_cmd_t *zc) 718{ 719 free((void *)(uintptr_t)zc->zc_nvlist_conf); 720 free((void *)(uintptr_t)zc->zc_nvlist_src); 721 free((void *)(uintptr_t)zc->zc_nvlist_dst); 722} 723 724static int 725zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 726 nvlist_t *nvl) 727{ 728 char *packed; 729 size_t len; 730 731 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 732 733 if ((packed = zfs_alloc(hdl, len)) == NULL) 734 return (-1); 735 736 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 737 738 *outnv = (uint64_t)(uintptr_t)packed; 739 *outlen = len; 740 741 return (0); 742} 743 744int 745zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 746{ 747 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 748 &zc->zc_nvlist_conf_size, nvl)); 749} 750 751int 752zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 753{ 754 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 755 &zc->zc_nvlist_src_size, nvl)); 756} 757 758/* 759 * Unpacks an nvlist from the ZFS ioctl command structure. 760 */ 761int 762zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 763{ 764 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 765 zc->zc_nvlist_dst_size, nvlp, 0) != 0) 766 return (no_memory(hdl)); 767 768 return (0); 769} 770 771int 772zfs_ioctl(libzfs_handle_t *hdl, unsigned long request, zfs_cmd_t *zc) 773{ 774 int error; 775 776 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 777 error = ioctl(hdl->libzfs_fd, request, zc); 778 if (hdl->libzfs_log_str) { 779 free(hdl->libzfs_log_str); 780 hdl->libzfs_log_str = NULL; 781 } 782 zc->zc_history = 0; 783 784 return (error); 785} 786 787/* 788 * ================================================================ 789 * API shared by zfs and zpool property management 790 * ================================================================ 791 */ 792 793static void 794zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 795{ 796 zprop_list_t *pl = cbp->cb_proplist; 797 int i; 798 char *title; 799 size_t len; 800 801 cbp->cb_first = B_FALSE; 802 if (cbp->cb_scripted) 803 return; 804 805 /* 806 * Start with the length of the column headers. 807 */ 808 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 809 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 810 "PROPERTY")); 811 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 812 "VALUE")); 813 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 814 "SOURCE")); 815 816 /* first property is always NAME */ 817 assert(cbp->cb_proplist->pl_prop == 818 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 819 820 /* 821 * Go through and calculate the widths for each column. For the 822 * 'source' column, we kludge it up by taking the worst-case scenario of 823 * inheriting from the longest name. This is acceptable because in the 824 * majority of cases 'SOURCE' is the last column displayed, and we don't 825 * use the width anyway. Note that the 'VALUE' column can be oversized, 826 * if the name of the property is much longer the any values we find. 827 */ 828 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 829 /* 830 * 'PROPERTY' column 831 */ 832 if (pl->pl_prop != ZPROP_INVAL) { 833 const char *propname = (type == ZFS_TYPE_POOL) ? 834 zpool_prop_to_name(pl->pl_prop) : 835 zfs_prop_to_name(pl->pl_prop); 836 837 len = strlen(propname); 838 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 839 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 840 } else { 841 len = strlen(pl->pl_user_prop); 842 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 843 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 844 } 845 846 /* 847 * 'VALUE' column. The first property is always the 'name' 848 * property that was tacked on either by /sbin/zfs's 849 * zfs_do_get() or when calling zprop_expand_list(), so we 850 * ignore its width. If the user specified the name property 851 * to display, then it will be later in the list in any case. 852 */ 853 if (pl != cbp->cb_proplist && 854 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 855 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 856 857 /* 858 * 'NAME' and 'SOURCE' columns 859 */ 860 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 861 ZFS_PROP_NAME) && 862 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 863 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 864 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 865 strlen(dgettext(TEXT_DOMAIN, "inherited from")); 866 } 867 } 868 869 /* 870 * Now go through and print the headers. 871 */ 872 for (i = 0; i < 4; i++) { 873 switch (cbp->cb_columns[i]) { 874 case GET_COL_NAME: 875 title = dgettext(TEXT_DOMAIN, "NAME"); 876 break; 877 case GET_COL_PROPERTY: 878 title = dgettext(TEXT_DOMAIN, "PROPERTY"); 879 break; 880 case GET_COL_VALUE: 881 title = dgettext(TEXT_DOMAIN, "VALUE"); 882 break; 883 case GET_COL_SOURCE: 884 title = dgettext(TEXT_DOMAIN, "SOURCE"); 885 break; 886 default: 887 title = NULL; 888 } 889 890 if (title != NULL) { 891 if (i == 3 || cbp->cb_columns[i + 1] == 0) 892 (void) printf("%s", title); 893 else 894 (void) printf("%-*s ", 895 cbp->cb_colwidths[cbp->cb_columns[i]], 896 title); 897 } 898 } 899 (void) printf("\n"); 900} 901 902/* 903 * Display a single line of output, according to the settings in the callback 904 * structure. 905 */ 906void 907zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 908 const char *propname, const char *value, zprop_source_t sourcetype, 909 const char *source) 910{ 911 int i; 912 const char *str; 913 char buf[128]; 914 915 /* 916 * Ignore those source types that the user has chosen to ignore. 917 */ 918 if ((sourcetype & cbp->cb_sources) == 0) 919 return; 920 921 if (cbp->cb_first) 922 zprop_print_headers(cbp, cbp->cb_type); 923 924 for (i = 0; i < 4; i++) { 925 switch (cbp->cb_columns[i]) { 926 case GET_COL_NAME: 927 str = name; 928 break; 929 930 case GET_COL_PROPERTY: 931 str = propname; 932 break; 933 934 case GET_COL_VALUE: 935 str = value; 936 break; 937 938 case GET_COL_SOURCE: 939 switch (sourcetype) { 940 case ZPROP_SRC_NONE: 941 str = "-"; 942 break; 943 944 case ZPROP_SRC_DEFAULT: 945 str = "default"; 946 break; 947 948 case ZPROP_SRC_LOCAL: 949 str = "local"; 950 break; 951 952 case ZPROP_SRC_TEMPORARY: 953 str = "temporary"; 954 break; 955 956 case ZPROP_SRC_INHERITED: 957 (void) snprintf(buf, sizeof (buf), 958 "inherited from %s", source); 959 str = buf; 960 break; 961 } 962 break; 963 964 default: 965 continue; 966 } 967 968 if (cbp->cb_columns[i + 1] == 0) 969 (void) printf("%s", str); 970 else if (cbp->cb_scripted) 971 (void) printf("%s\t", str); 972 else 973 (void) printf("%-*s ", 974 cbp->cb_colwidths[cbp->cb_columns[i]], 975 str); 976 977 } 978 979 (void) printf("\n"); 980} 981 982/* 983 * Given a numeric suffix, convert the value into a number of bits that the 984 * resulting value must be shifted. 985 */ 986static int 987str2shift(libzfs_handle_t *hdl, const char *buf) 988{ 989 const char *ends = "BKMGTPEZ"; 990 int i; 991 992 if (buf[0] == '\0') 993 return (0); 994 for (i = 0; i < strlen(ends); i++) { 995 if (toupper(buf[0]) == ends[i]) 996 break; 997 } 998 if (i == strlen(ends)) { 999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1000 "invalid numeric suffix '%s'"), buf); 1001 return (-1); 1002 } 1003 1004 /* 1005 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1006 * allow 'BB' - that's just weird. 1007 */ 1008 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1009 toupper(buf[0]) != 'B')) 1010 return (10*i); 1011 1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1013 "invalid numeric suffix '%s'"), buf); 1014 return (-1); 1015} 1016 1017/* 1018 * Convert a string of the form '100G' into a real number. Used when setting 1019 * properties or creating a volume. 'buf' is used to place an extended error 1020 * message for the caller to use. 1021 */ 1022int 1023zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1024{ 1025 char *end; 1026 int shift; 1027 1028 *num = 0; 1029 1030 /* Check to see if this looks like a number. */ 1031 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1032 if (hdl) 1033 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1034 "bad numeric value '%s'"), value); 1035 return (-1); 1036 } 1037 1038 /* Rely on stroull() to process the numeric portion. */ 1039 errno = 0; 1040 *num = strtoull(value, &end, 10); 1041 1042 /* 1043 * Check for ERANGE, which indicates that the value is too large to fit 1044 * in a 64-bit value. 1045 */ 1046 if (errno == ERANGE) { 1047 if (hdl) 1048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1049 "numeric value is too large")); 1050 return (-1); 1051 } 1052 1053 /* 1054 * If we have a decimal value, then do the computation with floating 1055 * point arithmetic. Otherwise, use standard arithmetic. 1056 */ 1057 if (*end == '.') { 1058 double fval = strtod(value, &end); 1059 1060 if ((shift = str2shift(hdl, end)) == -1) 1061 return (-1); 1062 1063 fval *= pow(2, shift); 1064 1065 if (fval > UINT64_MAX) { 1066 if (hdl) 1067 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1068 "numeric value is too large")); 1069 return (-1); 1070 } 1071 1072 *num = (uint64_t)fval; 1073 } else { 1074 if ((shift = str2shift(hdl, end)) == -1) 1075 return (-1); 1076 1077 /* Check for overflow */ 1078 if (shift >= 64 || (*num << shift) >> shift != *num) { 1079 if (hdl) 1080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1081 "numeric value is too large")); 1082 return (-1); 1083 } 1084 1085 *num <<= shift; 1086 } 1087 1088 return (0); 1089} 1090 1091/* 1092 * Given a propname=value nvpair to set, parse any numeric properties 1093 * (index, boolean, etc) if they are specified as strings and add the 1094 * resulting nvpair to the returned nvlist. 1095 * 1096 * At the DSL layer, all properties are either 64-bit numbers or strings. 1097 * We want the user to be able to ignore this fact and specify properties 1098 * as native values (numbers, for example) or as strings (to simplify 1099 * command line utilities). This also handles converting index types 1100 * (compression, checksum, etc) from strings to their on-disk index. 1101 */ 1102int 1103zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1104 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1105 const char *errbuf) 1106{ 1107 data_type_t datatype = nvpair_type(elem); 1108 zprop_type_t proptype; 1109 const char *propname; 1110 char *value; 1111 boolean_t isnone = B_FALSE; 1112 1113 if (type == ZFS_TYPE_POOL) { 1114 proptype = zpool_prop_get_type(prop); 1115 propname = zpool_prop_to_name(prop); 1116 } else { 1117 proptype = zfs_prop_get_type(prop); 1118 propname = zfs_prop_to_name(prop); 1119 } 1120 1121 /* 1122 * Convert any properties to the internal DSL value types. 1123 */ 1124 *svalp = NULL; 1125 *ivalp = 0; 1126 1127 switch (proptype) { 1128 case PROP_TYPE_STRING: 1129 if (datatype != DATA_TYPE_STRING) { 1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1131 "'%s' must be a string"), nvpair_name(elem)); 1132 goto error; 1133 } 1134 (void) nvpair_value_string(elem, svalp); 1135 if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1137 "'%s' is too long"), nvpair_name(elem)); 1138 goto error; 1139 } 1140 break; 1141 1142 case PROP_TYPE_NUMBER: 1143 if (datatype == DATA_TYPE_STRING) { 1144 (void) nvpair_value_string(elem, &value); 1145 if (strcmp(value, "none") == 0) { 1146 isnone = B_TRUE; 1147 } else if (zfs_nicestrtonum(hdl, value, ivalp) 1148 != 0) { 1149 goto error; 1150 } 1151 } else if (datatype == DATA_TYPE_UINT64) { 1152 (void) nvpair_value_uint64(elem, ivalp); 1153 } else { 1154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1155 "'%s' must be a number"), nvpair_name(elem)); 1156 goto error; 1157 } 1158 1159 /* 1160 * Quota special: force 'none' and don't allow 0. 1161 */ 1162 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1163 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1165 "use 'none' to disable quota/refquota")); 1166 goto error; 1167 } 1168 break; 1169 1170 case PROP_TYPE_INDEX: 1171 if (datatype != DATA_TYPE_STRING) { 1172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1173 "'%s' must be a string"), nvpair_name(elem)); 1174 goto error; 1175 } 1176 1177 (void) nvpair_value_string(elem, &value); 1178 1179 if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1181 "'%s' must be one of '%s'"), propname, 1182 zprop_values(prop, type)); 1183 goto error; 1184 } 1185 break; 1186 1187 default: 1188 abort(); 1189 } 1190 1191 /* 1192 * Add the result to our return set of properties. 1193 */ 1194 if (*svalp != NULL) { 1195 if (nvlist_add_string(ret, propname, *svalp) != 0) { 1196 (void) no_memory(hdl); 1197 return (-1); 1198 } 1199 } else { 1200 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1201 (void) no_memory(hdl); 1202 return (-1); 1203 } 1204 } 1205 1206 return (0); 1207error: 1208 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1209 return (-1); 1210} 1211 1212static int 1213addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 1214 zfs_type_t type) 1215{ 1216 int prop; 1217 zprop_list_t *entry; 1218 1219 prop = zprop_name_to_prop(propname, type); 1220 1221 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 1222 prop = ZPROP_INVAL; 1223 1224 /* 1225 * When no property table entry can be found, return failure if 1226 * this is a pool property or if this isn't a user-defined 1227 * dataset property, 1228 */ 1229 if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 1230 (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 1231 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1232 "invalid property '%s'"), propname); 1233 return (zfs_error(hdl, EZFS_BADPROP, 1234 dgettext(TEXT_DOMAIN, "bad property list"))); 1235 } 1236 1237 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1238 return (-1); 1239 1240 entry->pl_prop = prop; 1241 if (prop == ZPROP_INVAL) { 1242 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 1243 free(entry); 1244 return (-1); 1245 } 1246 entry->pl_width = strlen(propname); 1247 } else { 1248 entry->pl_width = zprop_width(prop, &entry->pl_fixed, 1249 type); 1250 } 1251 1252 *listp = entry; 1253 1254 return (0); 1255} 1256 1257/* 1258 * Given a comma-separated list of properties, construct a property list 1259 * containing both user-defined and native properties. This function will 1260 * return a NULL list if 'all' is specified, which can later be expanded 1261 * by zprop_expand_list(). 1262 */ 1263int 1264zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1265 zfs_type_t type) 1266{ 1267 *listp = NULL; 1268 1269 /* 1270 * If 'all' is specified, return a NULL list. 1271 */ 1272 if (strcmp(props, "all") == 0) 1273 return (0); 1274 1275 /* 1276 * If no props were specified, return an error. 1277 */ 1278 if (props[0] == '\0') { 1279 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1280 "no properties specified")); 1281 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1282 "bad property list"))); 1283 } 1284 1285 /* 1286 * It would be nice to use getsubopt() here, but the inclusion of column 1287 * aliases makes this more effort than it's worth. 1288 */ 1289 while (*props != '\0') { 1290 size_t len; 1291 char *p; 1292 char c; 1293 1294 if ((p = strchr(props, ',')) == NULL) { 1295 len = strlen(props); 1296 p = props + len; 1297 } else { 1298 len = p - props; 1299 } 1300 1301 /* 1302 * Check for empty options. 1303 */ 1304 if (len == 0) { 1305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1306 "empty property name")); 1307 return (zfs_error(hdl, EZFS_BADPROP, 1308 dgettext(TEXT_DOMAIN, "bad property list"))); 1309 } 1310 1311 /* 1312 * Check all regular property names. 1313 */ 1314 c = props[len]; 1315 props[len] = '\0'; 1316 1317 if (strcmp(props, "space") == 0) { 1318 static char *spaceprops[] = { 1319 "name", "avail", "used", "usedbysnapshots", 1320 "usedbydataset", "usedbyrefreservation", 1321 "usedbychildren", NULL 1322 }; 1323 int i; 1324 1325 for (i = 0; spaceprops[i]; i++) { 1326 if (addlist(hdl, spaceprops[i], listp, type)) 1327 return (-1); 1328 listp = &(*listp)->pl_next; 1329 } 1330 } else { 1331 if (addlist(hdl, props, listp, type)) 1332 return (-1); 1333 listp = &(*listp)->pl_next; 1334 } 1335 1336 props = p; 1337 if (c == ',') 1338 props++; 1339 } 1340 1341 return (0); 1342} 1343 1344void 1345zprop_free_list(zprop_list_t *pl) 1346{ 1347 zprop_list_t *next; 1348 1349 while (pl != NULL) { 1350 next = pl->pl_next; 1351 free(pl->pl_user_prop); 1352 free(pl); 1353 pl = next; 1354 } 1355} 1356 1357typedef struct expand_data { 1358 zprop_list_t **last; 1359 libzfs_handle_t *hdl; 1360 zfs_type_t type; 1361} expand_data_t; 1362 1363int 1364zprop_expand_list_cb(int prop, void *cb) 1365{ 1366 zprop_list_t *entry; 1367 expand_data_t *edp = cb; 1368 1369 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1370 return (ZPROP_INVAL); 1371 1372 entry->pl_prop = prop; 1373 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1374 entry->pl_all = B_TRUE; 1375 1376 *(edp->last) = entry; 1377 edp->last = &entry->pl_next; 1378 1379 return (ZPROP_CONT); 1380} 1381 1382int 1383zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1384{ 1385 zprop_list_t *entry; 1386 zprop_list_t **last; 1387 expand_data_t exp; 1388 1389 if (*plp == NULL) { 1390 /* 1391 * If this is the very first time we've been called for an 'all' 1392 * specification, expand the list to include all native 1393 * properties. 1394 */ 1395 last = plp; 1396 1397 exp.last = last; 1398 exp.hdl = hdl; 1399 exp.type = type; 1400 1401 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1402 B_FALSE, type) == ZPROP_INVAL) 1403 return (-1); 1404 1405 /* 1406 * Add 'name' to the beginning of the list, which is handled 1407 * specially. 1408 */ 1409 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1410 return (-1); 1411 1412 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1413 ZFS_PROP_NAME; 1414 entry->pl_width = zprop_width(entry->pl_prop, 1415 &entry->pl_fixed, type); 1416 entry->pl_all = B_TRUE; 1417 entry->pl_next = *plp; 1418 *plp = entry; 1419 } 1420 return (0); 1421} 1422 1423int 1424zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1425 zfs_type_t type) 1426{ 1427 return (zprop_iter_common(func, cb, show_all, ordered, type)); 1428}
| 216 case EZFS_UNKNOWN: 217 return (dgettext(TEXT_DOMAIN, "unknown error")); 218 default: 219 assert(hdl->libzfs_error == 0); 220 return (dgettext(TEXT_DOMAIN, "no error")); 221 } 222} 223 224/*PRINTFLIKE2*/ 225void 226zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 227{ 228 va_list ap; 229 230 va_start(ap, fmt); 231 232 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 233 fmt, ap); 234 hdl->libzfs_desc_active = 1; 235 236 va_end(ap); 237} 238 239static void 240zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 241{ 242 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 243 fmt, ap); 244 hdl->libzfs_error = error; 245 246 if (hdl->libzfs_desc_active) 247 hdl->libzfs_desc_active = 0; 248 else 249 hdl->libzfs_desc[0] = '\0'; 250 251 if (hdl->libzfs_printerr) { 252 if (error == EZFS_UNKNOWN) { 253 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 254 "error: %s\n"), libzfs_error_description(hdl)); 255 abort(); 256 } 257 258 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 259 libzfs_error_description(hdl)); 260 if (error == EZFS_NOMEM) 261 exit(1); 262 } 263} 264 265int 266zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 267{ 268 return (zfs_error_fmt(hdl, error, "%s", msg)); 269} 270 271/*PRINTFLIKE3*/ 272int 273zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 274{ 275 va_list ap; 276 277 va_start(ap, fmt); 278 279 zfs_verror(hdl, error, fmt, ap); 280 281 va_end(ap); 282 283 return (-1); 284} 285 286static int 287zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 288 va_list ap) 289{ 290 switch (error) { 291 case EPERM: 292 case EACCES: 293 zfs_verror(hdl, EZFS_PERM, fmt, ap); 294 return (-1); 295 296 case ECANCELED: 297 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 298 return (-1); 299 300 case EIO: 301 zfs_verror(hdl, EZFS_IO, fmt, ap); 302 return (-1); 303 304 case EINTR: 305 zfs_verror(hdl, EZFS_INTR, fmt, ap); 306 return (-1); 307 } 308 309 return (0); 310} 311 312int 313zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 314{ 315 return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 316} 317 318/*PRINTFLIKE3*/ 319int 320zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 321{ 322 va_list ap; 323 324 va_start(ap, fmt); 325 326 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 327 va_end(ap); 328 return (-1); 329 } 330 331 switch (error) { 332 case ENXIO: 333 case ENODEV: 334 zfs_verror(hdl, EZFS_IO, fmt, ap); 335 break; 336 337 case ENOENT: 338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 339 "dataset does not exist")); 340 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 341 break; 342 343 case ENOSPC: 344 case EDQUOT: 345 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 346 return (-1); 347 348 case EEXIST: 349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 350 "dataset already exists")); 351 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 352 break; 353 354 case EBUSY: 355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 356 "dataset is busy")); 357 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 358 break; 359 case EROFS: 360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 361 "snapshot permissions cannot be modified")); 362 zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 363 break; 364 case ENAMETOOLONG: 365 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 366 break; 367 case ENOTSUP: 368 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 369 break; 370 case EAGAIN: 371 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 372 "pool I/O is currently suspended")); 373 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 374 break; 375 default: 376 zfs_error_aux(hdl, strerror(errno)); 377 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 378 break; 379 } 380 381 va_end(ap); 382 return (-1); 383} 384 385int 386zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 387{ 388 return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 389} 390 391/*PRINTFLIKE3*/ 392int 393zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 394{ 395 va_list ap; 396 397 va_start(ap, fmt); 398 399 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 400 va_end(ap); 401 return (-1); 402 } 403 404 switch (error) { 405 case ENODEV: 406 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 407 break; 408 409 case ENOENT: 410 zfs_error_aux(hdl, 411 dgettext(TEXT_DOMAIN, "no such pool or dataset")); 412 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 413 break; 414 415 case EEXIST: 416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 417 "pool already exists")); 418 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 419 break; 420 421 case EBUSY: 422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 423 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 424 break; 425 426 case ENXIO: 427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 428 "one or more devices is currently unavailable")); 429 zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 430 break; 431 432 case ENAMETOOLONG: 433 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 434 break; 435 436 case ENOTSUP: 437 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 438 break; 439 440 case EINVAL: 441 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 442 break; 443 444 case ENOSPC: 445 case EDQUOT: 446 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 447 return (-1); 448 case EAGAIN: 449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 450 "pool I/O is currently suspended")); 451 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 452 break; 453 454 default: 455 zfs_error_aux(hdl, strerror(error)); 456 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 457 } 458 459 va_end(ap); 460 return (-1); 461} 462 463/* 464 * Display an out of memory error message and abort the current program. 465 */ 466int 467no_memory(libzfs_handle_t *hdl) 468{ 469 return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 470} 471 472/* 473 * A safe form of malloc() which will die if the allocation fails. 474 */ 475void * 476zfs_alloc(libzfs_handle_t *hdl, size_t size) 477{ 478 void *data; 479 480 if ((data = calloc(1, size)) == NULL) 481 (void) no_memory(hdl); 482 483 return (data); 484} 485 486/* 487 * A safe form of realloc(), which also zeroes newly allocated space. 488 */ 489void * 490zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 491{ 492 void *ret; 493 494 if ((ret = realloc(ptr, newsize)) == NULL) { 495 (void) no_memory(hdl); 496 return (NULL); 497 } 498 499 bzero((char *)ret + oldsize, (newsize - oldsize)); 500 return (ret); 501} 502 503/* 504 * A safe form of strdup() which will die if the allocation fails. 505 */ 506char * 507zfs_strdup(libzfs_handle_t *hdl, const char *str) 508{ 509 char *ret; 510 511 if ((ret = strdup(str)) == NULL) 512 (void) no_memory(hdl); 513 514 return (ret); 515} 516 517/* 518 * Convert a number to an appropriately human-readable output. 519 */ 520void 521zfs_nicenum(uint64_t num, char *buf, size_t buflen) 522{ 523 uint64_t n = num; 524 int index = 0; 525 char u; 526 527 while (n >= 1024) { 528 n /= 1024; 529 index++; 530 } 531 532 u = " KMGTPE"[index]; 533 534 if (index == 0) { 535 (void) snprintf(buf, buflen, "%llu", n); 536 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 537 /* 538 * If this is an even multiple of the base, always display 539 * without any decimal precision. 540 */ 541 (void) snprintf(buf, buflen, "%llu%c", n, u); 542 } else { 543 /* 544 * We want to choose a precision that reflects the best choice 545 * for fitting in 5 characters. This can get rather tricky when 546 * we have numbers that are very close to an order of magnitude. 547 * For example, when displaying 10239 (which is really 9.999K), 548 * we want only a single place of precision for 10.0K. We could 549 * develop some complex heuristics for this, but it's much 550 * easier just to try each combination in turn. 551 */ 552 int i; 553 for (i = 2; i >= 0; i--) { 554 if (snprintf(buf, buflen, "%.*f%c", i, 555 (double)num / (1ULL << 10 * index), u) <= 5) 556 break; 557 } 558 } 559} 560 561void 562libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 563{ 564 hdl->libzfs_printerr = printerr; 565} 566 567static int 568libzfs_load(void) 569{ 570 int error; 571 572 if (modfind("zfs") < 0) { 573 /* Not present in kernel, try loading it. */ 574 if (kldload("zfs") < 0 || modfind("zfs") < 0) { 575 if (errno != EEXIST) 576 return (error); 577 } 578 } 579 return (0); 580} 581 582libzfs_handle_t * 583libzfs_init(void) 584{ 585 libzfs_handle_t *hdl; 586 587 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 588 return (NULL); 589 } 590 591 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 592 if (libzfs_load() == 0) 593 hdl->libzfs_fd = open(ZFS_DEV, O_RDWR); 594 if (hdl->libzfs_fd < 0) { 595 free(hdl); 596 return (NULL); 597 } 598 } 599 600 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 601 (void) close(hdl->libzfs_fd); 602 free(hdl); 603 return (NULL); 604 } 605 606 hdl->libzfs_sharetab = fopen(ZFS_EXPORTS_PATH, "r"); 607 608 zfs_prop_init(); 609 zpool_prop_init(); 610 libzfs_mnttab_init(hdl); 611 612 return (hdl); 613} 614 615void 616libzfs_fini(libzfs_handle_t *hdl) 617{ 618 (void) close(hdl->libzfs_fd); 619 if (hdl->libzfs_mnttab) 620 (void) fclose(hdl->libzfs_mnttab); 621 if (hdl->libzfs_sharetab) 622 (void) fclose(hdl->libzfs_sharetab); 623 zfs_uninit_libshare(hdl); 624 if (hdl->libzfs_log_str) 625 (void) free(hdl->libzfs_log_str); 626 zpool_free_handles(hdl); 627 namespace_clear(hdl); 628 libzfs_mnttab_fini(hdl); 629 free(hdl); 630} 631 632libzfs_handle_t * 633zpool_get_handle(zpool_handle_t *zhp) 634{ 635 return (zhp->zpool_hdl); 636} 637 638libzfs_handle_t * 639zfs_get_handle(zfs_handle_t *zhp) 640{ 641 return (zhp->zfs_hdl); 642} 643 644zpool_handle_t * 645zfs_get_pool_handle(const zfs_handle_t *zhp) 646{ 647 return (zhp->zpool_hdl); 648} 649 650/* 651 * Given a name, determine whether or not it's a valid path 652 * (starts with '/' or "./"). If so, walk the mnttab trying 653 * to match the device number. If not, treat the path as an 654 * fs/vol/snap name. 655 */ 656zfs_handle_t * 657zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 658{ 659 struct statfs statbuf; 660 661 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 662 /* 663 * It's not a valid path, assume it's a name of type 'argtype'. 664 */ 665 return (zfs_open(hdl, path, argtype)); 666 } 667 668 if (statfs(path, &statbuf) != 0) { 669 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 670 return (NULL); 671 } 672 673 if (strcmp(statbuf.f_fstypename, MNTTYPE_ZFS) != 0) { 674 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 675 path); 676 return (NULL); 677 } 678 679 return (zfs_open(hdl, statbuf.f_mntfromname, ZFS_TYPE_FILESYSTEM)); 680} 681 682/* 683 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 684 * an ioctl(). 685 */ 686int 687zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 688{ 689 if (len == 0) 690 len = 2048; 691 zc->zc_nvlist_dst_size = len; 692 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 693 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == 0) 694 return (-1); 695 696 return (0); 697} 698 699/* 700 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 701 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 702 * filled in by the kernel to indicate the actual required size. 703 */ 704int 705zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 706{ 707 free((void *)(uintptr_t)zc->zc_nvlist_dst); 708 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 709 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 710 == 0) 711 return (-1); 712 713 return (0); 714} 715 716/* 717 * Called to free the src and dst nvlists stored in the command structure. 718 */ 719void 720zcmd_free_nvlists(zfs_cmd_t *zc) 721{ 722 free((void *)(uintptr_t)zc->zc_nvlist_conf); 723 free((void *)(uintptr_t)zc->zc_nvlist_src); 724 free((void *)(uintptr_t)zc->zc_nvlist_dst); 725} 726 727static int 728zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 729 nvlist_t *nvl) 730{ 731 char *packed; 732 size_t len; 733 734 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 735 736 if ((packed = zfs_alloc(hdl, len)) == NULL) 737 return (-1); 738 739 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 740 741 *outnv = (uint64_t)(uintptr_t)packed; 742 *outlen = len; 743 744 return (0); 745} 746 747int 748zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 749{ 750 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 751 &zc->zc_nvlist_conf_size, nvl)); 752} 753 754int 755zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 756{ 757 return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 758 &zc->zc_nvlist_src_size, nvl)); 759} 760 761/* 762 * Unpacks an nvlist from the ZFS ioctl command structure. 763 */ 764int 765zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 766{ 767 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 768 zc->zc_nvlist_dst_size, nvlp, 0) != 0) 769 return (no_memory(hdl)); 770 771 return (0); 772} 773 774int 775zfs_ioctl(libzfs_handle_t *hdl, unsigned long request, zfs_cmd_t *zc) 776{ 777 int error; 778 779 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 780 error = ioctl(hdl->libzfs_fd, request, zc); 781 if (hdl->libzfs_log_str) { 782 free(hdl->libzfs_log_str); 783 hdl->libzfs_log_str = NULL; 784 } 785 zc->zc_history = 0; 786 787 return (error); 788} 789 790/* 791 * ================================================================ 792 * API shared by zfs and zpool property management 793 * ================================================================ 794 */ 795 796static void 797zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 798{ 799 zprop_list_t *pl = cbp->cb_proplist; 800 int i; 801 char *title; 802 size_t len; 803 804 cbp->cb_first = B_FALSE; 805 if (cbp->cb_scripted) 806 return; 807 808 /* 809 * Start with the length of the column headers. 810 */ 811 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 812 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 813 "PROPERTY")); 814 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 815 "VALUE")); 816 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 817 "SOURCE")); 818 819 /* first property is always NAME */ 820 assert(cbp->cb_proplist->pl_prop == 821 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : ZFS_PROP_NAME)); 822 823 /* 824 * Go through and calculate the widths for each column. For the 825 * 'source' column, we kludge it up by taking the worst-case scenario of 826 * inheriting from the longest name. This is acceptable because in the 827 * majority of cases 'SOURCE' is the last column displayed, and we don't 828 * use the width anyway. Note that the 'VALUE' column can be oversized, 829 * if the name of the property is much longer the any values we find. 830 */ 831 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 832 /* 833 * 'PROPERTY' column 834 */ 835 if (pl->pl_prop != ZPROP_INVAL) { 836 const char *propname = (type == ZFS_TYPE_POOL) ? 837 zpool_prop_to_name(pl->pl_prop) : 838 zfs_prop_to_name(pl->pl_prop); 839 840 len = strlen(propname); 841 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 842 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 843 } else { 844 len = strlen(pl->pl_user_prop); 845 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 846 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 847 } 848 849 /* 850 * 'VALUE' column. The first property is always the 'name' 851 * property that was tacked on either by /sbin/zfs's 852 * zfs_do_get() or when calling zprop_expand_list(), so we 853 * ignore its width. If the user specified the name property 854 * to display, then it will be later in the list in any case. 855 */ 856 if (pl != cbp->cb_proplist && 857 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 858 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 859 860 /* 861 * 'NAME' and 'SOURCE' columns 862 */ 863 if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME : 864 ZFS_PROP_NAME) && 865 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 866 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 867 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 868 strlen(dgettext(TEXT_DOMAIN, "inherited from")); 869 } 870 } 871 872 /* 873 * Now go through and print the headers. 874 */ 875 for (i = 0; i < 4; i++) { 876 switch (cbp->cb_columns[i]) { 877 case GET_COL_NAME: 878 title = dgettext(TEXT_DOMAIN, "NAME"); 879 break; 880 case GET_COL_PROPERTY: 881 title = dgettext(TEXT_DOMAIN, "PROPERTY"); 882 break; 883 case GET_COL_VALUE: 884 title = dgettext(TEXT_DOMAIN, "VALUE"); 885 break; 886 case GET_COL_SOURCE: 887 title = dgettext(TEXT_DOMAIN, "SOURCE"); 888 break; 889 default: 890 title = NULL; 891 } 892 893 if (title != NULL) { 894 if (i == 3 || cbp->cb_columns[i + 1] == 0) 895 (void) printf("%s", title); 896 else 897 (void) printf("%-*s ", 898 cbp->cb_colwidths[cbp->cb_columns[i]], 899 title); 900 } 901 } 902 (void) printf("\n"); 903} 904 905/* 906 * Display a single line of output, according to the settings in the callback 907 * structure. 908 */ 909void 910zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 911 const char *propname, const char *value, zprop_source_t sourcetype, 912 const char *source) 913{ 914 int i; 915 const char *str; 916 char buf[128]; 917 918 /* 919 * Ignore those source types that the user has chosen to ignore. 920 */ 921 if ((sourcetype & cbp->cb_sources) == 0) 922 return; 923 924 if (cbp->cb_first) 925 zprop_print_headers(cbp, cbp->cb_type); 926 927 for (i = 0; i < 4; i++) { 928 switch (cbp->cb_columns[i]) { 929 case GET_COL_NAME: 930 str = name; 931 break; 932 933 case GET_COL_PROPERTY: 934 str = propname; 935 break; 936 937 case GET_COL_VALUE: 938 str = value; 939 break; 940 941 case GET_COL_SOURCE: 942 switch (sourcetype) { 943 case ZPROP_SRC_NONE: 944 str = "-"; 945 break; 946 947 case ZPROP_SRC_DEFAULT: 948 str = "default"; 949 break; 950 951 case ZPROP_SRC_LOCAL: 952 str = "local"; 953 break; 954 955 case ZPROP_SRC_TEMPORARY: 956 str = "temporary"; 957 break; 958 959 case ZPROP_SRC_INHERITED: 960 (void) snprintf(buf, sizeof (buf), 961 "inherited from %s", source); 962 str = buf; 963 break; 964 } 965 break; 966 967 default: 968 continue; 969 } 970 971 if (cbp->cb_columns[i + 1] == 0) 972 (void) printf("%s", str); 973 else if (cbp->cb_scripted) 974 (void) printf("%s\t", str); 975 else 976 (void) printf("%-*s ", 977 cbp->cb_colwidths[cbp->cb_columns[i]], 978 str); 979 980 } 981 982 (void) printf("\n"); 983} 984 985/* 986 * Given a numeric suffix, convert the value into a number of bits that the 987 * resulting value must be shifted. 988 */ 989static int 990str2shift(libzfs_handle_t *hdl, const char *buf) 991{ 992 const char *ends = "BKMGTPEZ"; 993 int i; 994 995 if (buf[0] == '\0') 996 return (0); 997 for (i = 0; i < strlen(ends); i++) { 998 if (toupper(buf[0]) == ends[i]) 999 break; 1000 } 1001 if (i == strlen(ends)) { 1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1003 "invalid numeric suffix '%s'"), buf); 1004 return (-1); 1005 } 1006 1007 /* 1008 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 1009 * allow 'BB' - that's just weird. 1010 */ 1011 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 1012 toupper(buf[0]) != 'B')) 1013 return (10*i); 1014 1015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1016 "invalid numeric suffix '%s'"), buf); 1017 return (-1); 1018} 1019 1020/* 1021 * Convert a string of the form '100G' into a real number. Used when setting 1022 * properties or creating a volume. 'buf' is used to place an extended error 1023 * message for the caller to use. 1024 */ 1025int 1026zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1027{ 1028 char *end; 1029 int shift; 1030 1031 *num = 0; 1032 1033 /* Check to see if this looks like a number. */ 1034 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1035 if (hdl) 1036 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1037 "bad numeric value '%s'"), value); 1038 return (-1); 1039 } 1040 1041 /* Rely on stroull() to process the numeric portion. */ 1042 errno = 0; 1043 *num = strtoull(value, &end, 10); 1044 1045 /* 1046 * Check for ERANGE, which indicates that the value is too large to fit 1047 * in a 64-bit value. 1048 */ 1049 if (errno == ERANGE) { 1050 if (hdl) 1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1052 "numeric value is too large")); 1053 return (-1); 1054 } 1055 1056 /* 1057 * If we have a decimal value, then do the computation with floating 1058 * point arithmetic. Otherwise, use standard arithmetic. 1059 */ 1060 if (*end == '.') { 1061 double fval = strtod(value, &end); 1062 1063 if ((shift = str2shift(hdl, end)) == -1) 1064 return (-1); 1065 1066 fval *= pow(2, shift); 1067 1068 if (fval > UINT64_MAX) { 1069 if (hdl) 1070 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1071 "numeric value is too large")); 1072 return (-1); 1073 } 1074 1075 *num = (uint64_t)fval; 1076 } else { 1077 if ((shift = str2shift(hdl, end)) == -1) 1078 return (-1); 1079 1080 /* Check for overflow */ 1081 if (shift >= 64 || (*num << shift) >> shift != *num) { 1082 if (hdl) 1083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1084 "numeric value is too large")); 1085 return (-1); 1086 } 1087 1088 *num <<= shift; 1089 } 1090 1091 return (0); 1092} 1093 1094/* 1095 * Given a propname=value nvpair to set, parse any numeric properties 1096 * (index, boolean, etc) if they are specified as strings and add the 1097 * resulting nvpair to the returned nvlist. 1098 * 1099 * At the DSL layer, all properties are either 64-bit numbers or strings. 1100 * We want the user to be able to ignore this fact and specify properties 1101 * as native values (numbers, for example) or as strings (to simplify 1102 * command line utilities). This also handles converting index types 1103 * (compression, checksum, etc) from strings to their on-disk index. 1104 */ 1105int 1106zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1107 zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp, 1108 const char *errbuf) 1109{ 1110 data_type_t datatype = nvpair_type(elem); 1111 zprop_type_t proptype; 1112 const char *propname; 1113 char *value; 1114 boolean_t isnone = B_FALSE; 1115 1116 if (type == ZFS_TYPE_POOL) { 1117 proptype = zpool_prop_get_type(prop); 1118 propname = zpool_prop_to_name(prop); 1119 } else { 1120 proptype = zfs_prop_get_type(prop); 1121 propname = zfs_prop_to_name(prop); 1122 } 1123 1124 /* 1125 * Convert any properties to the internal DSL value types. 1126 */ 1127 *svalp = NULL; 1128 *ivalp = 0; 1129 1130 switch (proptype) { 1131 case PROP_TYPE_STRING: 1132 if (datatype != DATA_TYPE_STRING) { 1133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1134 "'%s' must be a string"), nvpair_name(elem)); 1135 goto error; 1136 } 1137 (void) nvpair_value_string(elem, svalp); 1138 if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1140 "'%s' is too long"), nvpair_name(elem)); 1141 goto error; 1142 } 1143 break; 1144 1145 case PROP_TYPE_NUMBER: 1146 if (datatype == DATA_TYPE_STRING) { 1147 (void) nvpair_value_string(elem, &value); 1148 if (strcmp(value, "none") == 0) { 1149 isnone = B_TRUE; 1150 } else if (zfs_nicestrtonum(hdl, value, ivalp) 1151 != 0) { 1152 goto error; 1153 } 1154 } else if (datatype == DATA_TYPE_UINT64) { 1155 (void) nvpair_value_uint64(elem, ivalp); 1156 } else { 1157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1158 "'%s' must be a number"), nvpair_name(elem)); 1159 goto error; 1160 } 1161 1162 /* 1163 * Quota special: force 'none' and don't allow 0. 1164 */ 1165 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1166 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1168 "use 'none' to disable quota/refquota")); 1169 goto error; 1170 } 1171 break; 1172 1173 case PROP_TYPE_INDEX: 1174 if (datatype != DATA_TYPE_STRING) { 1175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1176 "'%s' must be a string"), nvpair_name(elem)); 1177 goto error; 1178 } 1179 1180 (void) nvpair_value_string(elem, &value); 1181 1182 if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1184 "'%s' must be one of '%s'"), propname, 1185 zprop_values(prop, type)); 1186 goto error; 1187 } 1188 break; 1189 1190 default: 1191 abort(); 1192 } 1193 1194 /* 1195 * Add the result to our return set of properties. 1196 */ 1197 if (*svalp != NULL) { 1198 if (nvlist_add_string(ret, propname, *svalp) != 0) { 1199 (void) no_memory(hdl); 1200 return (-1); 1201 } 1202 } else { 1203 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1204 (void) no_memory(hdl); 1205 return (-1); 1206 } 1207 } 1208 1209 return (0); 1210error: 1211 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1212 return (-1); 1213} 1214 1215static int 1216addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp, 1217 zfs_type_t type) 1218{ 1219 int prop; 1220 zprop_list_t *entry; 1221 1222 prop = zprop_name_to_prop(propname, type); 1223 1224 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type)) 1225 prop = ZPROP_INVAL; 1226 1227 /* 1228 * When no property table entry can be found, return failure if 1229 * this is a pool property or if this isn't a user-defined 1230 * dataset property, 1231 */ 1232 if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL || 1233 (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) { 1234 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1235 "invalid property '%s'"), propname); 1236 return (zfs_error(hdl, EZFS_BADPROP, 1237 dgettext(TEXT_DOMAIN, "bad property list"))); 1238 } 1239 1240 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1241 return (-1); 1242 1243 entry->pl_prop = prop; 1244 if (prop == ZPROP_INVAL) { 1245 if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) { 1246 free(entry); 1247 return (-1); 1248 } 1249 entry->pl_width = strlen(propname); 1250 } else { 1251 entry->pl_width = zprop_width(prop, &entry->pl_fixed, 1252 type); 1253 } 1254 1255 *listp = entry; 1256 1257 return (0); 1258} 1259 1260/* 1261 * Given a comma-separated list of properties, construct a property list 1262 * containing both user-defined and native properties. This function will 1263 * return a NULL list if 'all' is specified, which can later be expanded 1264 * by zprop_expand_list(). 1265 */ 1266int 1267zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1268 zfs_type_t type) 1269{ 1270 *listp = NULL; 1271 1272 /* 1273 * If 'all' is specified, return a NULL list. 1274 */ 1275 if (strcmp(props, "all") == 0) 1276 return (0); 1277 1278 /* 1279 * If no props were specified, return an error. 1280 */ 1281 if (props[0] == '\0') { 1282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1283 "no properties specified")); 1284 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1285 "bad property list"))); 1286 } 1287 1288 /* 1289 * It would be nice to use getsubopt() here, but the inclusion of column 1290 * aliases makes this more effort than it's worth. 1291 */ 1292 while (*props != '\0') { 1293 size_t len; 1294 char *p; 1295 char c; 1296 1297 if ((p = strchr(props, ',')) == NULL) { 1298 len = strlen(props); 1299 p = props + len; 1300 } else { 1301 len = p - props; 1302 } 1303 1304 /* 1305 * Check for empty options. 1306 */ 1307 if (len == 0) { 1308 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1309 "empty property name")); 1310 return (zfs_error(hdl, EZFS_BADPROP, 1311 dgettext(TEXT_DOMAIN, "bad property list"))); 1312 } 1313 1314 /* 1315 * Check all regular property names. 1316 */ 1317 c = props[len]; 1318 props[len] = '\0'; 1319 1320 if (strcmp(props, "space") == 0) { 1321 static char *spaceprops[] = { 1322 "name", "avail", "used", "usedbysnapshots", 1323 "usedbydataset", "usedbyrefreservation", 1324 "usedbychildren", NULL 1325 }; 1326 int i; 1327 1328 for (i = 0; spaceprops[i]; i++) { 1329 if (addlist(hdl, spaceprops[i], listp, type)) 1330 return (-1); 1331 listp = &(*listp)->pl_next; 1332 } 1333 } else { 1334 if (addlist(hdl, props, listp, type)) 1335 return (-1); 1336 listp = &(*listp)->pl_next; 1337 } 1338 1339 props = p; 1340 if (c == ',') 1341 props++; 1342 } 1343 1344 return (0); 1345} 1346 1347void 1348zprop_free_list(zprop_list_t *pl) 1349{ 1350 zprop_list_t *next; 1351 1352 while (pl != NULL) { 1353 next = pl->pl_next; 1354 free(pl->pl_user_prop); 1355 free(pl); 1356 pl = next; 1357 } 1358} 1359 1360typedef struct expand_data { 1361 zprop_list_t **last; 1362 libzfs_handle_t *hdl; 1363 zfs_type_t type; 1364} expand_data_t; 1365 1366int 1367zprop_expand_list_cb(int prop, void *cb) 1368{ 1369 zprop_list_t *entry; 1370 expand_data_t *edp = cb; 1371 1372 if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL) 1373 return (ZPROP_INVAL); 1374 1375 entry->pl_prop = prop; 1376 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 1377 entry->pl_all = B_TRUE; 1378 1379 *(edp->last) = entry; 1380 edp->last = &entry->pl_next; 1381 1382 return (ZPROP_CONT); 1383} 1384 1385int 1386zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 1387{ 1388 zprop_list_t *entry; 1389 zprop_list_t **last; 1390 expand_data_t exp; 1391 1392 if (*plp == NULL) { 1393 /* 1394 * If this is the very first time we've been called for an 'all' 1395 * specification, expand the list to include all native 1396 * properties. 1397 */ 1398 last = plp; 1399 1400 exp.last = last; 1401 exp.hdl = hdl; 1402 exp.type = type; 1403 1404 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 1405 B_FALSE, type) == ZPROP_INVAL) 1406 return (-1); 1407 1408 /* 1409 * Add 'name' to the beginning of the list, which is handled 1410 * specially. 1411 */ 1412 if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL) 1413 return (-1); 1414 1415 entry->pl_prop = (type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1416 ZFS_PROP_NAME; 1417 entry->pl_width = zprop_width(entry->pl_prop, 1418 &entry->pl_fixed, type); 1419 entry->pl_all = B_TRUE; 1420 entry->pl_next = *plp; 1421 *plp = entry; 1422 } 1423 return (0); 1424} 1425 1426int 1427zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 1428 zfs_type_t type) 1429{ 1430 return (zprop_iter_common(func, cb, show_all, ordered, type)); 1431}
|