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