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