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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Internal utility routines for the ZFS library. 30 */ 31 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <libintl.h> 35 #include <stdarg.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <strings.h> 39 #include <unistd.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_VOLHASDATA: 98 return (dgettext(TEXT_DOMAIN, "volume has data")); 99 case EZFS_INVALIDNAME: 100 return (dgettext(TEXT_DOMAIN, "invalid name")); 101 case EZFS_BADRESTORE: 102 return (dgettext(TEXT_DOMAIN, "unable to restore to " 103 "destination")); 104 case EZFS_BADBACKUP: 105 return (dgettext(TEXT_DOMAIN, "backup failed")); 106 case EZFS_BADTARGET: 107 return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 108 case EZFS_NODEVICE: 109 return (dgettext(TEXT_DOMAIN, "no such device in pool")); 110 case EZFS_BADDEV: 111 return (dgettext(TEXT_DOMAIN, "invalid device")); 112 case EZFS_NOREPLICAS: 113 return (dgettext(TEXT_DOMAIN, "no valid replicas")); 114 case EZFS_RESILVERING: 115 return (dgettext(TEXT_DOMAIN, "currently resilvering")); 116 case EZFS_BADVERSION: 117 return (dgettext(TEXT_DOMAIN, "unsupported version")); 118 case EZFS_POOLUNAVAIL: 119 return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 120 case EZFS_DEVOVERFLOW: 121 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 122 case EZFS_BADPATH: 123 return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 124 case EZFS_CROSSTARGET: 125 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 126 "pools")); 127 case EZFS_ZONED: 128 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 129 case EZFS_MOUNTFAILED: 130 return (dgettext(TEXT_DOMAIN, "mount failed")); 131 case EZFS_UMOUNTFAILED: 132 return (dgettext(TEXT_DOMAIN, "umount failed")); 133 case EZFS_UNSHARENFSFAILED: 134 return (dgettext(TEXT_DOMAIN, "unshare(1M) failed")); 135 case EZFS_SHARENFSFAILED: 136 return (dgettext(TEXT_DOMAIN, "share(1M) failed")); 137 case EZFS_ISCSISVCUNAVAIL: 138 return (dgettext(TEXT_DOMAIN, 139 "iscsitgt service need to be enabled by " 140 "a privileged user")); 141 case EZFS_DEVLINKS: 142 return (dgettext(TEXT_DOMAIN, "failed to create /dev links")); 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_IO: 148 return (dgettext(TEXT_DOMAIN, "I/O error")); 149 case EZFS_INTR: 150 return (dgettext(TEXT_DOMAIN, "signal received")); 151 case EZFS_ISSPARE: 152 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 153 "spare")); 154 case EZFS_INVALCONFIG: 155 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 156 case EZFS_RECURSIVE: 157 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 158 case EZFS_NOHISTORY: 159 return (dgettext(TEXT_DOMAIN, "no history available")); 160 case EZFS_UNSHAREISCSIFAILED: 161 return (dgettext(TEXT_DOMAIN, 162 "iscsitgtd failed request to unshare")); 163 case EZFS_SHAREISCSIFAILED: 164 return (dgettext(TEXT_DOMAIN, 165 "iscsitgtd failed request to share")); 166 case EZFS_POOLPROPS: 167 return (dgettext(TEXT_DOMAIN, "failed to retrieve " 168 "pool properties")); 169 case EZFS_POOL_NOTSUP: 170 return (dgettext(TEXT_DOMAIN, "operation not supported " 171 "on this type of pool")); 172 case EZFS_POOL_INVALARG: 173 return (dgettext(TEXT_DOMAIN, "invalid argument for " 174 "this pool operation")); 175 case EZFS_NAMETOOLONG: 176 return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 177 case EZFS_OPENFAILED: 178 return (dgettext(TEXT_DOMAIN, "open failed")); 179 case EZFS_NOCAP: 180 return (dgettext(TEXT_DOMAIN, 181 "disk capacity information could not be retrieved")); 182 case EZFS_LABELFAILED: 183 return (dgettext(TEXT_DOMAIN, "write of label failed")); 184 case EZFS_BADWHO: 185 return (dgettext(TEXT_DOMAIN, "invalid user/group")); 186 case EZFS_BADPERM: 187 return (dgettext(TEXT_DOMAIN, "invalid permission")); 188 case EZFS_BADPERMSET: 189 return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 190 case EZFS_NODELEGATION: 191 return (dgettext(TEXT_DOMAIN, "delegated administration is " 192 "disabled on pool")); 193 case EZFS_PERMRDONLY: 194 return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be" 195 " modified")); 196 case EZFS_UNKNOWN: 197 return (dgettext(TEXT_DOMAIN, "unknown error")); 198 default: 199 assert(hdl->libzfs_error == 0); 200 return (dgettext(TEXT_DOMAIN, "no error")); 201 } 202 } 203 204 /*PRINTFLIKE2*/ 205 void 206 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 207 { 208 va_list ap; 209 210 va_start(ap, fmt); 211 212 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 213 fmt, ap); 214 hdl->libzfs_desc_active = 1; 215 216 va_end(ap); 217 } 218 219 static void 220 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 221 { 222 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 223 fmt, ap); 224 hdl->libzfs_error = error; 225 226 if (hdl->libzfs_desc_active) 227 hdl->libzfs_desc_active = 0; 228 else 229 hdl->libzfs_desc[0] = '\0'; 230 231 if (hdl->libzfs_printerr) { 232 if (error == EZFS_UNKNOWN) { 233 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 234 "error: %s\n"), libzfs_error_description(hdl)); 235 abort(); 236 } 237 238 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 239 libzfs_error_description(hdl)); 240 if (error == EZFS_NOMEM) 241 exit(1); 242 } 243 } 244 245 int 246 zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 247 { 248 return (zfs_error_fmt(hdl, error, "%s", msg)); 249 } 250 251 /*PRINTFLIKE3*/ 252 int 253 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 254 { 255 va_list ap; 256 257 va_start(ap, fmt); 258 259 zfs_verror(hdl, error, fmt, ap); 260 261 va_end(ap); 262 263 return (-1); 264 } 265 266 static int 267 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 268 va_list ap) 269 { 270 switch (error) { 271 case EPERM: 272 case EACCES: 273 zfs_verror(hdl, EZFS_PERM, fmt, ap); 274 return (-1); 275 276 case ECANCELED: 277 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 278 return (-1); 279 280 case EIO: 281 zfs_verror(hdl, EZFS_IO, fmt, ap); 282 return (-1); 283 284 case EINTR: 285 zfs_verror(hdl, EZFS_INTR, fmt, ap); 286 return (-1); 287 } 288 289 return (0); 290 } 291 292 int 293 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 294 { 295 return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 296 } 297 298 /*PRINTFLIKE3*/ 299 int 300 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 301 { 302 va_list ap; 303 304 va_start(ap, fmt); 305 306 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 307 va_end(ap); 308 return (-1); 309 } 310 311 312 switch (error) { 313 case ENXIO: 314 zfs_verror(hdl, EZFS_IO, fmt, ap); 315 break; 316 317 case ENOENT: 318 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 319 "dataset does not exist")); 320 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 321 break; 322 323 case ENOSPC: 324 case EDQUOT: 325 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 326 return (-1); 327 328 case EEXIST: 329 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 330 "dataset already exists")); 331 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 332 break; 333 334 case EBUSY: 335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 336 "dataset is busy")); 337 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 338 break; 339 case EROFS: 340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 341 "snapshot permissions cannot be modified")); 342 zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap); 343 break; 344 case ENAMETOOLONG: 345 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 346 break; 347 default: 348 zfs_error_aux(hdl, strerror(errno)); 349 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 350 break; 351 } 352 353 va_end(ap); 354 return (-1); 355 } 356 357 int 358 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 359 { 360 return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 361 } 362 363 /*PRINTFLIKE3*/ 364 int 365 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 366 { 367 va_list ap; 368 369 va_start(ap, fmt); 370 371 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 372 va_end(ap); 373 return (-1); 374 } 375 376 switch (error) { 377 case ENODEV: 378 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 379 break; 380 381 case ENOENT: 382 zfs_error_aux(hdl, 383 dgettext(TEXT_DOMAIN, "no such pool or dataset")); 384 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 385 break; 386 387 case EEXIST: 388 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 389 "pool already exists")); 390 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 391 break; 392 393 case EBUSY: 394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 395 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 396 break; 397 398 case ENXIO: 399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 400 "one or more devices is currently unavailable")); 401 zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 402 break; 403 404 case ENAMETOOLONG: 405 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 406 break; 407 408 case ENOTSUP: 409 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 410 break; 411 412 case EINVAL: 413 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 414 break; 415 416 case ENOSPC: 417 case EDQUOT: 418 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 419 return (-1); 420 421 default: 422 zfs_error_aux(hdl, strerror(error)); 423 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 424 } 425 426 va_end(ap); 427 return (-1); 428 } 429 430 /* 431 * Display an out of memory error message and abort the current program. 432 */ 433 int 434 no_memory(libzfs_handle_t *hdl) 435 { 436 return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 437 } 438 439 /* 440 * A safe form of malloc() which will die if the allocation fails. 441 */ 442 void * 443 zfs_alloc(libzfs_handle_t *hdl, size_t size) 444 { 445 void *data; 446 447 if ((data = calloc(1, size)) == NULL) 448 (void) no_memory(hdl); 449 450 return (data); 451 } 452 453 /* 454 * A safe form of realloc(), which also zeroes newly allocated space. 455 */ 456 void * 457 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 458 { 459 void *ret; 460 461 if ((ret = realloc(ptr, newsize)) == NULL) { 462 (void) no_memory(hdl); 463 free(ptr); 464 return (NULL); 465 } 466 467 bzero((char *)ret + oldsize, (newsize - oldsize)); 468 return (ret); 469 } 470 471 /* 472 * A safe form of strdup() which will die if the allocation fails. 473 */ 474 char * 475 zfs_strdup(libzfs_handle_t *hdl, const char *str) 476 { 477 char *ret; 478 479 if ((ret = strdup(str)) == NULL) 480 (void) no_memory(hdl); 481 482 return (ret); 483 } 484 485 /* 486 * Convert a number to an appropriately human-readable output. 487 */ 488 void 489 zfs_nicenum(uint64_t num, char *buf, size_t buflen) 490 { 491 uint64_t n = num; 492 int index = 0; 493 char u; 494 495 while (n >= 1024) { 496 n /= 1024; 497 index++; 498 } 499 500 u = " KMGTPE"[index]; 501 502 if (index == 0) { 503 (void) snprintf(buf, buflen, "%llu", n); 504 } else if ((num & ((1ULL << 10 * index) - 1)) == 0) { 505 /* 506 * If this is an even multiple of the base, always display 507 * without any decimal precision. 508 */ 509 (void) snprintf(buf, buflen, "%llu%c", n, u); 510 } else { 511 /* 512 * We want to choose a precision that reflects the best choice 513 * for fitting in 5 characters. This can get rather tricky when 514 * we have numbers that are very close to an order of magnitude. 515 * For example, when displaying 10239 (which is really 9.999K), 516 * we want only a single place of precision for 10.0K. We could 517 * develop some complex heuristics for this, but it's much 518 * easier just to try each combination in turn. 519 */ 520 int i; 521 for (i = 2; i >= 0; i--) { 522 if (snprintf(buf, buflen, "%.*f%c", i, 523 (double)num / (1ULL << 10 * index), u) <= 5) 524 break; 525 } 526 } 527 } 528 529 void 530 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 531 { 532 hdl->libzfs_printerr = printerr; 533 } 534 535 libzfs_handle_t * 536 libzfs_init(void) 537 { 538 libzfs_handle_t *hdl; 539 540 if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) { 541 return (NULL); 542 } 543 544 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) { 545 free(hdl); 546 return (NULL); 547 } 548 549 if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) { 550 (void) close(hdl->libzfs_fd); 551 free(hdl); 552 return (NULL); 553 } 554 555 hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r"); 556 557 zfs_prop_init(); 558 559 return (hdl); 560 } 561 562 void 563 libzfs_fini(libzfs_handle_t *hdl) 564 { 565 (void) close(hdl->libzfs_fd); 566 if (hdl->libzfs_mnttab) 567 (void) fclose(hdl->libzfs_mnttab); 568 if (hdl->libzfs_sharetab) 569 (void) fclose(hdl->libzfs_sharetab); 570 zfs_uninit_libshare(hdl); 571 if (hdl->libzfs_log_str) 572 (void) free(hdl->libzfs_log_str); 573 namespace_clear(hdl); 574 free(hdl); 575 } 576 577 libzfs_handle_t * 578 zpool_get_handle(zpool_handle_t *zhp) 579 { 580 return (zhp->zpool_hdl); 581 } 582 583 libzfs_handle_t * 584 zfs_get_handle(zfs_handle_t *zhp) 585 { 586 return (zhp->zfs_hdl); 587 } 588 589 /* 590 * Given a name, determine whether or not it's a valid path 591 * (starts with '/' or "./"). If so, walk the mnttab trying 592 * to match the device number. If not, treat the path as an 593 * fs/vol/snap name. 594 */ 595 zfs_handle_t * 596 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype) 597 { 598 struct stat64 statbuf; 599 struct extmnttab entry; 600 int ret; 601 602 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 603 /* 604 * It's not a valid path, assume it's a name of type 'argtype'. 605 */ 606 return (zfs_open(hdl, path, argtype)); 607 } 608 609 if (stat64(path, &statbuf) != 0) { 610 (void) fprintf(stderr, "%s: %s\n", path, strerror(errno)); 611 return (NULL); 612 } 613 614 rewind(hdl->libzfs_mnttab); 615 while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) { 616 if (makedevice(entry.mnt_major, entry.mnt_minor) == 617 statbuf.st_dev) { 618 break; 619 } 620 } 621 if (ret != 0) { 622 return (NULL); 623 } 624 625 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 626 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 627 path); 628 return (NULL); 629 } 630 631 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 632 } 633 634 /* 635 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 636 * an ioctl(). 637 */ 638 int 639 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 640 { 641 if (len == 0) 642 len = 2048; 643 zc->zc_nvlist_dst_size = len; 644 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 645 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL) 646 return (-1); 647 648 return (0); 649 } 650 651 /* 652 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 653 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 654 * filled in by the kernel to indicate the actual required size. 655 */ 656 int 657 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 658 { 659 free((void *)(uintptr_t)zc->zc_nvlist_dst); 660 if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t) 661 zfs_alloc(hdl, zc->zc_nvlist_dst_size)) 662 == NULL) 663 return (-1); 664 665 return (0); 666 } 667 668 /* 669 * Called to free the src and dst nvlists stored in the command structure. 670 */ 671 void 672 zcmd_free_nvlists(zfs_cmd_t *zc) 673 { 674 free((void *)(uintptr_t)zc->zc_nvlist_src); 675 free((void *)(uintptr_t)zc->zc_nvlist_dst); 676 } 677 678 int 679 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl, 680 size_t *size) 681 { 682 char *packed; 683 size_t len; 684 685 verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0); 686 687 if ((packed = zfs_alloc(hdl, len)) == NULL) 688 return (-1); 689 690 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 691 692 zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed; 693 zc->zc_nvlist_src_size = len; 694 695 if (size) 696 *size = len; 697 return (0); 698 } 699 700 /* 701 * Unpacks an nvlist from the ZFS ioctl command structure. 702 */ 703 int 704 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 705 { 706 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 707 zc->zc_nvlist_dst_size, nvlp, 0) != 0) 708 return (no_memory(hdl)); 709 710 return (0); 711 } 712 713 static void 714 zfs_print_prop_headers(libzfs_get_cbdata_t *cbp) 715 { 716 zfs_proplist_t *pl = cbp->cb_proplist; 717 int i; 718 char *title; 719 size_t len; 720 721 cbp->cb_first = B_FALSE; 722 if (cbp->cb_scripted) 723 return; 724 725 /* 726 * Start with the length of the column headers. 727 */ 728 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 729 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 730 "PROPERTY")); 731 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 732 "VALUE")); 733 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 734 "SOURCE")); 735 736 /* 737 * Go through and calculate the widths for each column. For the 738 * 'source' column, we kludge it up by taking the worst-case scenario of 739 * inheriting from the longest name. This is acceptable because in the 740 * majority of cases 'SOURCE' is the last column displayed, and we don't 741 * use the width anyway. Note that the 'VALUE' column can be oversized, 742 * if the name of the property is much longer the any values we find. 743 */ 744 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 745 /* 746 * 'PROPERTY' column 747 */ 748 if (pl->pl_prop != ZFS_PROP_INVAL) { 749 len = strlen(zfs_prop_to_name(pl->pl_prop)); 750 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 751 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 752 } else { 753 len = strlen(pl->pl_user_prop); 754 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 755 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 756 } 757 758 /* 759 * 'VALUE' column 760 */ 761 if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) && 762 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 763 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 764 765 /* 766 * 'NAME' and 'SOURCE' columns 767 */ 768 if (pl->pl_prop == ZFS_PROP_NAME && 769 pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) { 770 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 771 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 772 strlen(dgettext(TEXT_DOMAIN, "inherited from")); 773 } 774 } 775 776 /* 777 * Now go through and print the headers. 778 */ 779 for (i = 0; i < 4; i++) { 780 switch (cbp->cb_columns[i]) { 781 case GET_COL_NAME: 782 title = dgettext(TEXT_DOMAIN, "NAME"); 783 break; 784 case GET_COL_PROPERTY: 785 title = dgettext(TEXT_DOMAIN, "PROPERTY"); 786 break; 787 case GET_COL_VALUE: 788 title = dgettext(TEXT_DOMAIN, "VALUE"); 789 break; 790 case GET_COL_SOURCE: 791 title = dgettext(TEXT_DOMAIN, "SOURCE"); 792 break; 793 default: 794 title = NULL; 795 } 796 797 if (title != NULL) { 798 if (i == 3 || cbp->cb_columns[i + 1] == 0) 799 (void) printf("%s", title); 800 else 801 (void) printf("%-*s ", 802 cbp->cb_colwidths[cbp->cb_columns[i]], 803 title); 804 } 805 } 806 (void) printf("\n"); 807 } 808 809 /* 810 * Display a single line of output, according to the settings in the callback 811 * structure. 812 */ 813 void 814 libzfs_print_one_property(const char *name, libzfs_get_cbdata_t *cbp, 815 const char *propname, const char *value, zfs_source_t sourcetype, 816 const char *source) 817 { 818 int i; 819 const char *str; 820 char buf[128]; 821 822 /* 823 * Ignore those source types that the user has chosen to ignore. 824 */ 825 if ((sourcetype & cbp->cb_sources) == 0) 826 return; 827 828 if (cbp->cb_first) 829 zfs_print_prop_headers(cbp); 830 831 for (i = 0; i < 4; i++) { 832 switch (cbp->cb_columns[i]) { 833 case GET_COL_NAME: 834 str = name; 835 break; 836 837 case GET_COL_PROPERTY: 838 str = propname; 839 break; 840 841 case GET_COL_VALUE: 842 str = value; 843 break; 844 845 case GET_COL_SOURCE: 846 switch (sourcetype) { 847 case ZFS_SRC_NONE: 848 str = "-"; 849 break; 850 851 case ZFS_SRC_DEFAULT: 852 str = "default"; 853 break; 854 855 case ZFS_SRC_LOCAL: 856 str = "local"; 857 break; 858 859 case ZFS_SRC_TEMPORARY: 860 str = "temporary"; 861 break; 862 863 case ZFS_SRC_INHERITED: 864 (void) snprintf(buf, sizeof (buf), 865 "inherited from %s", source); 866 str = buf; 867 break; 868 } 869 break; 870 871 default: 872 continue; 873 } 874 875 if (cbp->cb_columns[i + 1] == 0) 876 (void) printf("%s", str); 877 else if (cbp->cb_scripted) 878 (void) printf("%s\t", str); 879 else 880 (void) printf("%-*s ", 881 cbp->cb_colwidths[cbp->cb_columns[i]], 882 str); 883 884 } 885 886 (void) printf("\n"); 887 } 888 889 int 890 zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc) 891 { 892 int error; 893 894 zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str; 895 error = ioctl(hdl->libzfs_fd, request, zc); 896 if (hdl->libzfs_log_str) { 897 free(hdl->libzfs_log_str); 898 hdl->libzfs_log_str = NULL; 899 } 900 zc->zc_history = 0; 901 902 return (error); 903 } 904