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