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