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