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