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