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