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