1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens /*
26789Sahrens  * Internal utility routines for the ZFS library.
27789Sahrens  */
28789Sahrens 
29789Sahrens #include <errno.h>
30789Sahrens #include <fcntl.h>
31789Sahrens #include <libintl.h>
32789Sahrens #include <stdarg.h>
33789Sahrens #include <stdio.h>
34789Sahrens #include <stdlib.h>
35789Sahrens #include <strings.h>
36789Sahrens #include <unistd.h>
375094Slling #include <ctype.h>
385094Slling #include <math.h>
39789Sahrens #include <sys/mnttab.h>
403635Sck153898 #include <sys/mntent.h>
413635Sck153898 #include <sys/types.h>
42789Sahrens 
43789Sahrens #include <libzfs.h>
44789Sahrens 
45789Sahrens #include "libzfs_impl.h"
464787Sahrens #include "zfs_prop.h"
47789Sahrens 
482082Seschrock int
492082Seschrock libzfs_errno(libzfs_handle_t *hdl)
502082Seschrock {
512082Seschrock 	return (hdl->libzfs_error);
522082Seschrock }
53789Sahrens 
542082Seschrock const char *
552082Seschrock libzfs_error_action(libzfs_handle_t *hdl)
562082Seschrock {
572082Seschrock 	return (hdl->libzfs_action);
582082Seschrock }
592082Seschrock 
602082Seschrock const char *
612082Seschrock libzfs_error_description(libzfs_handle_t *hdl)
622082Seschrock {
632082Seschrock 	if (hdl->libzfs_desc[0] != '\0')
642082Seschrock 		return (hdl->libzfs_desc);
65789Sahrens 
662082Seschrock 	switch (hdl->libzfs_error) {
672082Seschrock 	case EZFS_NOMEM:
682082Seschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
692082Seschrock 	case EZFS_BADPROP:
702082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
712082Seschrock 	case EZFS_PROPREADONLY:
722082Seschrock 		return (dgettext(TEXT_DOMAIN, "read only property"));
732082Seschrock 	case EZFS_PROPTYPE:
742082Seschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
752082Seschrock 		    "datasets of this type"));
762082Seschrock 	case EZFS_PROPNONINHERIT:
772082Seschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
782082Seschrock 	case EZFS_PROPSPACE:
792082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
802082Seschrock 	case EZFS_BADTYPE:
812082Seschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
822082Seschrock 		    "datasets of this type"));
832082Seschrock 	case EZFS_BUSY:
842082Seschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
852082Seschrock 	case EZFS_EXISTS:
862082Seschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
872082Seschrock 	case EZFS_NOENT:
882082Seschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
892082Seschrock 	case EZFS_BADSTREAM:
902082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
912082Seschrock 	case EZFS_DSREADONLY:
922082Seschrock 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
932082Seschrock 	case EZFS_VOLTOOBIG:
942082Seschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
952082Seschrock 		    "this system"));
962082Seschrock 	case EZFS_INVALIDNAME:
972082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
982082Seschrock 	case EZFS_BADRESTORE:
992082Seschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
1002082Seschrock 		    "destination"));
1012082Seschrock 	case EZFS_BADBACKUP:
1022082Seschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
1032082Seschrock 	case EZFS_BADTARGET:
1042082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
1052082Seschrock 	case EZFS_NODEVICE:
1062082Seschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
1072082Seschrock 	case EZFS_BADDEV:
1082082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
1092082Seschrock 	case EZFS_NOREPLICAS:
1102082Seschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
1112082Seschrock 	case EZFS_RESILVERING:
1122082Seschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
1132082Seschrock 	case EZFS_BADVERSION:
1142082Seschrock 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
1152082Seschrock 	case EZFS_POOLUNAVAIL:
1162082Seschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
1172082Seschrock 	case EZFS_DEVOVERFLOW:
1182082Seschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
1192082Seschrock 	case EZFS_BADPATH:
1202082Seschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
1212082Seschrock 	case EZFS_CROSSTARGET:
1222082Seschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
1232082Seschrock 		    "pools"));
1242082Seschrock 	case EZFS_ZONED:
1252082Seschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
1262082Seschrock 	case EZFS_MOUNTFAILED:
1272082Seschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
1282082Seschrock 	case EZFS_UMOUNTFAILED:
1292082Seschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
1303126Sahl 	case EZFS_UNSHARENFSFAILED:
1312082Seschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
1323126Sahl 	case EZFS_SHARENFSFAILED:
1332082Seschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
1345331Samw 	case EZFS_UNSHARESMBFAILED:
1355331Samw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
1365331Samw 	case EZFS_SHARESMBFAILED:
1375331Samw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
1382082Seschrock 	case EZFS_PERM:
1392082Seschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
1402082Seschrock 	case EZFS_NOSPC:
1412082Seschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
14211807SSam.Falkner@Sun.COM 	case EZFS_FAULT:
14311807SSam.Falkner@Sun.COM 		return (dgettext(TEXT_DOMAIN, "bad address"));
1442082Seschrock 	case EZFS_IO:
1452082Seschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
1462082Seschrock 	case EZFS_INTR:
1472082Seschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
1482082Seschrock 	case EZFS_ISSPARE:
1492082Seschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
1502082Seschrock 		    "spare"));
1512082Seschrock 	case EZFS_INVALCONFIG:
1522082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1532474Seschrock 	case EZFS_RECURSIVE:
1542474Seschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
1552926Sek110237 	case EZFS_NOHISTORY:
1562926Sek110237 		return (dgettext(TEXT_DOMAIN, "no history available"));
1573912Slling 	case EZFS_POOLPROPS:
1583912Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
1593912Slling 		    "pool properties"));
1603912Slling 	case EZFS_POOL_NOTSUP:
1613912Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
1623912Slling 		    "on this type of pool"));
1633912Slling 	case EZFS_POOL_INVALARG:
1643912Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
1653912Slling 		    "this pool operation"));
1663978Smmusante 	case EZFS_NAMETOOLONG:
1673978Smmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1684276Staylor 	case EZFS_OPENFAILED:
1694276Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1704276Staylor 	case EZFS_NOCAP:
1714276Staylor 		return (dgettext(TEXT_DOMAIN,
1724276Staylor 		    "disk capacity information could not be retrieved"));
1734276Staylor 	case EZFS_LABELFAILED:
1744276Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
1754543Smarks 	case EZFS_BADWHO:
1764543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
1774543Smarks 	case EZFS_BADPERM:
1784543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
1794543Smarks 	case EZFS_BADPERMSET:
1804543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
1814543Smarks 	case EZFS_NODELEGATION:
1824543Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
1834543Smarks 		    "disabled on pool"));
1844543Smarks 	case EZFS_PERMRDONLY:
1854543Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
1864543Smarks 		    " modified"));
1875363Seschrock 	case EZFS_BADCACHE:
1885363Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
1895450Sbrendan 	case EZFS_ISL2CACHE:
1905450Sbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
1916423Sgw25295 	case EZFS_VDEVNOTSUP:
1926423Sgw25295 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
1936423Sgw25295 		    "supported"));
1947042Sgw25295 	case EZFS_NOTSUP:
1957042Sgw25295 		return (dgettext(TEXT_DOMAIN, "operation not supported "
1967042Sgw25295 		    "on this dataset"));
1977214Slling 	case EZFS_ACTIVE_SPARE:
1987214Slling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
1997214Slling 		    "device"));
2009701SGeorge.Wilson@Sun.COM 	case EZFS_UNPLAYED_LOGS:
2019701SGeorge.Wilson@Sun.COM 		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
2029701SGeorge.Wilson@Sun.COM 		    "logs"));
20310242Schris.kirby@sun.com 	case EZFS_REFTAG_RELE:
20410242Schris.kirby@sun.com 		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
20510242Schris.kirby@sun.com 	case EZFS_REFTAG_HOLD:
20610242Schris.kirby@sun.com 		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
20710242Schris.kirby@sun.com 		    "dataset"));
20810342Schris.kirby@sun.com 	case EZFS_TAGTOOLONG:
20910342Schris.kirby@sun.com 		return (dgettext(TEXT_DOMAIN, "tag too long"));
21011007SLori.Alt@Sun.COM 	case EZFS_PIPEFAILED:
21111007SLori.Alt@Sun.COM 		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
21211007SLori.Alt@Sun.COM 	case EZFS_THREADCREATEFAILED:
21311007SLori.Alt@Sun.COM 		return (dgettext(TEXT_DOMAIN, "thread create failed"));
21411422SMark.Musante@Sun.COM 	case EZFS_POSTSPLIT_ONLINE:
21511422SMark.Musante@Sun.COM 		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
21611422SMark.Musante@Sun.COM 		    "into a new one"));
21712296SLin.Ling@Sun.COM 	case EZFS_SCRUBBING:
21812296SLin.Ling@Sun.COM 		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
21912296SLin.Ling@Sun.COM 		    "use 'zpool scrub -s' to cancel current scrub"));
22012296SLin.Ling@Sun.COM 	case EZFS_NO_SCRUB:
22112296SLin.Ling@Sun.COM 		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
222*13043STim.Haley@Sun.COM 	case EZFS_DIFF:
223*13043STim.Haley@Sun.COM 		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
224*13043STim.Haley@Sun.COM 	case EZFS_DIFFDATA:
225*13043STim.Haley@Sun.COM 		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
2262082Seschrock 	case EZFS_UNKNOWN:
2272082Seschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
2282082Seschrock 	default:
2292500Seschrock 		assert(hdl->libzfs_error == 0);
2302500Seschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
2312082Seschrock 	}
2322082Seschrock }
2332082Seschrock 
2342082Seschrock /*PRINTFLIKE2*/
235789Sahrens void
2362082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
237789Sahrens {
238789Sahrens 	va_list ap;
239789Sahrens 
240789Sahrens 	va_start(ap, fmt);
241789Sahrens 
2422082Seschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
2432082Seschrock 	    fmt, ap);
2442082Seschrock 	hdl->libzfs_desc_active = 1;
245789Sahrens 
246789Sahrens 	va_end(ap);
247789Sahrens }
248789Sahrens 
2492082Seschrock static void
2502082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
2512082Seschrock {
2522082Seschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
2532082Seschrock 	    fmt, ap);
2542082Seschrock 	hdl->libzfs_error = error;
2552082Seschrock 
2562082Seschrock 	if (hdl->libzfs_desc_active)
2572082Seschrock 		hdl->libzfs_desc_active = 0;
2582082Seschrock 	else
2592082Seschrock 		hdl->libzfs_desc[0] = '\0';
2602082Seschrock 
2612082Seschrock 	if (hdl->libzfs_printerr) {
2622082Seschrock 		if (error == EZFS_UNKNOWN) {
2632082Seschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
2642082Seschrock 			    "error: %s\n"), libzfs_error_description(hdl));
2652082Seschrock 			abort();
2662082Seschrock 		}
2672082Seschrock 
2682082Seschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
2693912Slling 		    libzfs_error_description(hdl));
2702082Seschrock 		if (error == EZFS_NOMEM)
2712082Seschrock 			exit(1);
2722082Seschrock 	}
2732082Seschrock }
2742082Seschrock 
2753237Slling int
2763237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
2773237Slling {
2783237Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
2793237Slling }
2803237Slling 
2812082Seschrock /*PRINTFLIKE3*/
2822082Seschrock int
2833237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
2842082Seschrock {
2852082Seschrock 	va_list ap;
2862082Seschrock 
2872082Seschrock 	va_start(ap, fmt);
2882082Seschrock 
2892082Seschrock 	zfs_verror(hdl, error, fmt, ap);
2902082Seschrock 
2912082Seschrock 	va_end(ap);
2922082Seschrock 
2932082Seschrock 	return (-1);
2942082Seschrock }
2952082Seschrock 
2962082Seschrock static int
2972082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
2982082Seschrock     va_list ap)
2992082Seschrock {
3002082Seschrock 	switch (error) {
3012082Seschrock 	case EPERM:
3022082Seschrock 	case EACCES:
3032082Seschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
3042082Seschrock 		return (-1);
3052082Seschrock 
3064543Smarks 	case ECANCELED:
3074543Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
3084543Smarks 		return (-1);
3094543Smarks 
3102082Seschrock 	case EIO:
3112082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3122082Seschrock 		return (-1);
3132082Seschrock 
31411807SSam.Falkner@Sun.COM 	case EFAULT:
31511807SSam.Falkner@Sun.COM 		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
31611807SSam.Falkner@Sun.COM 		return (-1);
31711807SSam.Falkner@Sun.COM 
3182082Seschrock 	case EINTR:
3192082Seschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
3202082Seschrock 		return (-1);
3212082Seschrock 	}
3222082Seschrock 
3232082Seschrock 	return (0);
3242082Seschrock }
3252082Seschrock 
3263237Slling int
3273237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3283237Slling {
3293237Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
3303237Slling }
3313237Slling 
3322082Seschrock /*PRINTFLIKE3*/
3332082Seschrock int
3343237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
335789Sahrens {
336789Sahrens 	va_list ap;
337789Sahrens 
338789Sahrens 	va_start(ap, fmt);
339789Sahrens 
3402082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3412082Seschrock 		va_end(ap);
3422082Seschrock 		return (-1);
3432082Seschrock 	}
3442082Seschrock 
3452082Seschrock 	switch (error) {
3462082Seschrock 	case ENXIO:
3475807Sck153898 	case ENODEV:
3482082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3492082Seschrock 		break;
3502082Seschrock 
3512082Seschrock 	case ENOENT:
3522082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3532082Seschrock 		    "dataset does not exist"));
3542082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3552082Seschrock 		break;
3562082Seschrock 
3572082Seschrock 	case ENOSPC:
3582082Seschrock 	case EDQUOT:
3592082Seschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
3602082Seschrock 		return (-1);
3612082Seschrock 
3622082Seschrock 	case EEXIST:
3632082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3642082Seschrock 		    "dataset already exists"));
3652082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3662082Seschrock 		break;
3672082Seschrock 
3682082Seschrock 	case EBUSY:
3692082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3702082Seschrock 		    "dataset is busy"));
3712082Seschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
3722082Seschrock 		break;
3734543Smarks 	case EROFS:
3744543Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3754543Smarks 		    "snapshot permissions cannot be modified"));
3764543Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
3774543Smarks 		break;
3783978Smmusante 	case ENAMETOOLONG:
3793978Smmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
3803978Smmusante 		break;
3815860Sck153898 	case ENOTSUP:
3825860Sck153898 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
3835860Sck153898 		break;
3849234SGeorge.Wilson@Sun.COM 	case EAGAIN:
3859234SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3869234SGeorge.Wilson@Sun.COM 		    "pool I/O is currently suspended"));
3879234SGeorge.Wilson@Sun.COM 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
3889234SGeorge.Wilson@Sun.COM 		break;
3892082Seschrock 	default:
39011022STom.Erickson@Sun.COM 		zfs_error_aux(hdl, strerror(error));
3912082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
3922082Seschrock 		break;
393789Sahrens 	}
394789Sahrens 
395789Sahrens 	va_end(ap);
3962082Seschrock 	return (-1);
397789Sahrens }
398789Sahrens 
3993237Slling int
4003237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
4013237Slling {
4023237Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
4033237Slling }
4043237Slling 
4052082Seschrock /*PRINTFLIKE3*/
4062082Seschrock int
4073237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
408789Sahrens {
4092082Seschrock 	va_list ap;
4102082Seschrock 
4112082Seschrock 	va_start(ap, fmt);
4122082Seschrock 
4132082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
4142082Seschrock 		va_end(ap);
4152082Seschrock 		return (-1);
4162082Seschrock 	}
4172082Seschrock 
4182082Seschrock 	switch (error) {
4192082Seschrock 	case ENODEV:
4202082Seschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
4212082Seschrock 		break;
4222082Seschrock 
4232082Seschrock 	case ENOENT:
4243912Slling 		zfs_error_aux(hdl,
4253912Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
4262082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
4272082Seschrock 		break;
4282082Seschrock 
4292082Seschrock 	case EEXIST:
4302082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4312082Seschrock 		    "pool already exists"));
4322082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
4332082Seschrock 		break;
4342082Seschrock 
4352082Seschrock 	case EBUSY:
4362082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
4377341SEric.Schrock@Sun.COM 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
4382082Seschrock 		break;
4392082Seschrock 
4402082Seschrock 	case ENXIO:
4412082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4422082Seschrock 		    "one or more devices is currently unavailable"));
4432082Seschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
4442082Seschrock 		break;
4452082Seschrock 
4462082Seschrock 	case ENAMETOOLONG:
4472082Seschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
4482082Seschrock 		break;
4492082Seschrock 
4503912Slling 	case ENOTSUP:
4513912Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
4523912Slling 		break;
4533912Slling 
4543912Slling 	case EINVAL:
4553912Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
4563912Slling 		break;
4573912Slling 
4584543Smarks 	case ENOSPC:
4594543Smarks 	case EDQUOT:
4604543Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
4614543Smarks 		return (-1);
4629234SGeorge.Wilson@Sun.COM 	case EAGAIN:
4639234SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4649234SGeorge.Wilson@Sun.COM 		    "pool I/O is currently suspended"));
4659234SGeorge.Wilson@Sun.COM 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
4669234SGeorge.Wilson@Sun.COM 		break;
4674543Smarks 
4682082Seschrock 	default:
4692082Seschrock 		zfs_error_aux(hdl, strerror(error));
4702082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
4712082Seschrock 	}
4722082Seschrock 
4732082Seschrock 	va_end(ap);
4742082Seschrock 	return (-1);
475789Sahrens }
476789Sahrens 
477789Sahrens /*
478789Sahrens  * Display an out of memory error message and abort the current program.
479789Sahrens  */
4802082Seschrock int
4812082Seschrock no_memory(libzfs_handle_t *hdl)
482789Sahrens {
4832082Seschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
484789Sahrens }
485789Sahrens 
486789Sahrens /*
487789Sahrens  * A safe form of malloc() which will die if the allocation fails.
488789Sahrens  */
489789Sahrens void *
4902082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
491789Sahrens {
492789Sahrens 	void *data;
493789Sahrens 
494789Sahrens 	if ((data = calloc(1, size)) == NULL)
4952082Seschrock 		(void) no_memory(hdl);
496789Sahrens 
497789Sahrens 	return (data);
498789Sahrens }
499789Sahrens 
500789Sahrens /*
501*13043STim.Haley@Sun.COM  * A safe form of asprintf() which will die if the allocation fails.
502*13043STim.Haley@Sun.COM  */
503*13043STim.Haley@Sun.COM /*PRINTFLIKE2*/
504*13043STim.Haley@Sun.COM char *
505*13043STim.Haley@Sun.COM zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
506*13043STim.Haley@Sun.COM {
507*13043STim.Haley@Sun.COM 	va_list ap;
508*13043STim.Haley@Sun.COM 	char *ret;
509*13043STim.Haley@Sun.COM 	int err;
510*13043STim.Haley@Sun.COM 
511*13043STim.Haley@Sun.COM 	va_start(ap, fmt);
512*13043STim.Haley@Sun.COM 
513*13043STim.Haley@Sun.COM 	err = vasprintf(&ret, fmt, ap);
514*13043STim.Haley@Sun.COM 
515*13043STim.Haley@Sun.COM 	va_end(ap);
516*13043STim.Haley@Sun.COM 
517*13043STim.Haley@Sun.COM 	if (err < 0)
518*13043STim.Haley@Sun.COM 		(void) no_memory(hdl);
519*13043STim.Haley@Sun.COM 
520*13043STim.Haley@Sun.COM 	return (ret);
521*13043STim.Haley@Sun.COM }
522*13043STim.Haley@Sun.COM 
523*13043STim.Haley@Sun.COM /*
5242676Seschrock  * A safe form of realloc(), which also zeroes newly allocated space.
5252676Seschrock  */
5262676Seschrock void *
5272676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
5282676Seschrock {
5292676Seschrock 	void *ret;
5302676Seschrock 
5312676Seschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
5322676Seschrock 		(void) no_memory(hdl);
5332676Seschrock 		return (NULL);
5342676Seschrock 	}
5352676Seschrock 
5362676Seschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
5372676Seschrock 	return (ret);
5382676Seschrock }
5392676Seschrock 
5402676Seschrock /*
541789Sahrens  * A safe form of strdup() which will die if the allocation fails.
542789Sahrens  */
543789Sahrens char *
5442082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
545789Sahrens {
546789Sahrens 	char *ret;
547789Sahrens 
548789Sahrens 	if ((ret = strdup(str)) == NULL)
5492082Seschrock 		(void) no_memory(hdl);
550789Sahrens 
551789Sahrens 	return (ret);
552789Sahrens }
553789Sahrens 
554789Sahrens /*
555789Sahrens  * Convert a number to an appropriately human-readable output.
556789Sahrens  */
557789Sahrens void
558789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
559789Sahrens {
560789Sahrens 	uint64_t n = num;
561789Sahrens 	int index = 0;
562789Sahrens 	char u;
563789Sahrens 
564789Sahrens 	while (n >= 1024) {
5651162Seschrock 		n /= 1024;
566789Sahrens 		index++;
567789Sahrens 	}
568789Sahrens 
569789Sahrens 	u = " KMGTPE"[index];
570789Sahrens 
5711162Seschrock 	if (index == 0) {
572789Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5731162Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5741162Seschrock 		/*
5751162Seschrock 		 * If this is an even multiple of the base, always display
5761162Seschrock 		 * without any decimal precision.
5771162Seschrock 		 */
578789Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5791162Seschrock 	} else {
5801162Seschrock 		/*
5811162Seschrock 		 * We want to choose a precision that reflects the best choice
5821162Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5831162Seschrock 		 * we have numbers that are very close to an order of magnitude.
5841162Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5851162Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5861162Seschrock 		 * develop some complex heuristics for this, but it's much
5871162Seschrock 		 * easier just to try each combination in turn.
5881162Seschrock 		 */
5891162Seschrock 		int i;
5901162Seschrock 		for (i = 2; i >= 0; i--) {
5914451Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5924451Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5931162Seschrock 				break;
5941162Seschrock 		}
5951162Seschrock 	}
596789Sahrens }
5972082Seschrock 
5982082Seschrock void
5992082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
6002082Seschrock {
6012082Seschrock 	hdl->libzfs_printerr = printerr;
6022082Seschrock }
6032082Seschrock 
6042082Seschrock libzfs_handle_t *
6052082Seschrock libzfs_init(void)
6062082Seschrock {
6072082Seschrock 	libzfs_handle_t *hdl;
6082082Seschrock 
609*13043STim.Haley@Sun.COM 	if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
6102082Seschrock 		return (NULL);
6112082Seschrock 	}
6122082Seschrock 
6132500Seschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
6142082Seschrock 		free(hdl);
6152082Seschrock 		return (NULL);
6162082Seschrock 	}
6172082Seschrock 
6182082Seschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
6192082Seschrock 		(void) close(hdl->libzfs_fd);
6202082Seschrock 		free(hdl);
6212082Seschrock 		return (NULL);
6222082Seschrock 	}
6232082Seschrock 
6242082Seschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
6252082Seschrock 
6264787Sahrens 	zfs_prop_init();
6275094Slling 	zpool_prop_init();
6288811SEric.Taylor@Sun.COM 	libzfs_mnttab_init(hdl);
6294787Sahrens 
6302082Seschrock 	return (hdl);
6312082Seschrock }
6322082Seschrock 
6332082Seschrock void
6342082Seschrock libzfs_fini(libzfs_handle_t *hdl)
6352082Seschrock {
6362082Seschrock 	(void) close(hdl->libzfs_fd);
6372082Seschrock 	if (hdl->libzfs_mnttab)
6382082Seschrock 		(void) fclose(hdl->libzfs_mnttab);
6392082Seschrock 	if (hdl->libzfs_sharetab)
6402082Seschrock 		(void) fclose(hdl->libzfs_sharetab);
6414180Sdougm 	zfs_uninit_libshare(hdl);
6424543Smarks 	if (hdl->libzfs_log_str)
6434543Smarks 		(void) free(hdl->libzfs_log_str);
6446865Srm160521 	zpool_free_handles(hdl);
64510817SEric.Schrock@Sun.COM 	libzfs_fru_clear(hdl, B_TRUE);
6462082Seschrock 	namespace_clear(hdl);
6478811SEric.Taylor@Sun.COM 	libzfs_mnttab_fini(hdl);
6482082Seschrock 	free(hdl);
6492082Seschrock }
6502082Seschrock 
6512082Seschrock libzfs_handle_t *
6522082Seschrock zpool_get_handle(zpool_handle_t *zhp)
6532082Seschrock {
6542082Seschrock 	return (zhp->zpool_hdl);
6552082Seschrock }
6562082Seschrock 
6572082Seschrock libzfs_handle_t *
6582082Seschrock zfs_get_handle(zfs_handle_t *zhp)
6592082Seschrock {
6602082Seschrock 	return (zhp->zfs_hdl);
6612082Seschrock }
6622676Seschrock 
6637538SRichard.Morris@Sun.COM zpool_handle_t *
6647538SRichard.Morris@Sun.COM zfs_get_pool_handle(const zfs_handle_t *zhp)
6657538SRichard.Morris@Sun.COM {
6667538SRichard.Morris@Sun.COM 	return (zhp->zpool_hdl);
6677538SRichard.Morris@Sun.COM }
6687538SRichard.Morris@Sun.COM 
6692676Seschrock /*
6703635Sck153898  * Given a name, determine whether or not it's a valid path
6713635Sck153898  * (starts with '/' or "./").  If so, walk the mnttab trying
6723635Sck153898  * to match the device number.  If not, treat the path as an
6733635Sck153898  * fs/vol/snap name.
6743635Sck153898  */
6753635Sck153898 zfs_handle_t *
6763635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6773635Sck153898 {
6783635Sck153898 	struct stat64 statbuf;
6793635Sck153898 	struct extmnttab entry;
6803635Sck153898 	int ret;
6813635Sck153898 
6823635Sck153898 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6833635Sck153898 		/*
6843635Sck153898 		 * It's not a valid path, assume it's a name of type 'argtype'.
6853635Sck153898 		 */
6863635Sck153898 		return (zfs_open(hdl, path, argtype));
6873635Sck153898 	}
6883635Sck153898 
6893635Sck153898 	if (stat64(path, &statbuf) != 0) {
6903635Sck153898 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6913635Sck153898 		return (NULL);
6923635Sck153898 	}
6933635Sck153898 
6943635Sck153898 	rewind(hdl->libzfs_mnttab);
6953635Sck153898 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6963635Sck153898 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6973635Sck153898 		    statbuf.st_dev) {
6983635Sck153898 			break;
6993635Sck153898 		}
7003635Sck153898 	}
7013635Sck153898 	if (ret != 0) {
7023635Sck153898 		return (NULL);
7033635Sck153898 	}
7043635Sck153898 
7053635Sck153898 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7063635Sck153898 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
7073635Sck153898 		    path);
7083635Sck153898 		return (NULL);
7093635Sck153898 	}
7103635Sck153898 
7113635Sck153898 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
7123635Sck153898 }
7133635Sck153898 
7143635Sck153898 /*
7152676Seschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
7162676Seschrock  * an ioctl().
7172676Seschrock  */
7182676Seschrock int
7192676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
7202676Seschrock {
7212676Seschrock 	if (len == 0)
72212949SGeorge.Wilson@Sun.COM 		len = 16 * 1024;
7232676Seschrock 	zc->zc_nvlist_dst_size = len;
7242676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
7252676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
7262676Seschrock 		return (-1);
7272676Seschrock 
7282676Seschrock 	return (0);
7292676Seschrock }
7302676Seschrock 
7312676Seschrock /*
7322676Seschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
7332676Seschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
7342676Seschrock  * filled in by the kernel to indicate the actual required size.
7352676Seschrock  */
7362676Seschrock int
7372676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
7382676Seschrock {
7392676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
7402676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
7412676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
7422676Seschrock 	    == NULL)
7432676Seschrock 		return (-1);
7442676Seschrock 
7452676Seschrock 	return (0);
7462676Seschrock }
7472676Seschrock 
7482676Seschrock /*
7492885Sahrens  * Called to free the src and dst nvlists stored in the command structure.
7502676Seschrock  */
7512676Seschrock void
7522676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc)
7532676Seschrock {
7545094Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
7552676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
7562676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
7572676Seschrock }
7582676Seschrock 
7595094Slling static int
7605094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
7615094Slling     nvlist_t *nvl)
7622676Seschrock {
7632676Seschrock 	char *packed;
7642676Seschrock 	size_t len;
7652676Seschrock 
7662676Seschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
7672676Seschrock 
7682676Seschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
7692676Seschrock 		return (-1);
7702676Seschrock 
7712676Seschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
7722676Seschrock 
7735094Slling 	*outnv = (uint64_t)(uintptr_t)packed;
7745094Slling 	*outlen = len;
7755094Slling 
7765094Slling 	return (0);
7775094Slling }
7782676Seschrock 
7795094Slling int
7805094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7815094Slling {
7825094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
7835094Slling 	    &zc->zc_nvlist_conf_size, nvl));
7845094Slling }
7855094Slling 
7865094Slling int
7875094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7885094Slling {
7895094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
7905094Slling 	    &zc->zc_nvlist_src_size, nvl));
7912676Seschrock }
7922676Seschrock 
7932676Seschrock /*
7942676Seschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
7952676Seschrock  */
7962676Seschrock int
7972676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
7982676Seschrock {
7992676Seschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
8002676Seschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
8012676Seschrock 		return (no_memory(hdl));
8022676Seschrock 
8032676Seschrock 	return (0);
8042676Seschrock }
8053912Slling 
8065094Slling int
8075094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
8085094Slling {
8095094Slling 	int error;
8105094Slling 
8115094Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
8125094Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
8135094Slling 	if (hdl->libzfs_log_str) {
8145094Slling 		free(hdl->libzfs_log_str);
8155094Slling 		hdl->libzfs_log_str = NULL;
8165094Slling 	}
8175094Slling 	zc->zc_history = 0;
8185094Slling 
8195094Slling 	return (error);
8205094Slling }
8215094Slling 
8225094Slling /*
8235094Slling  * ================================================================
8245094Slling  * API shared by zfs and zpool property management
8255094Slling  * ================================================================
8265094Slling  */
8275094Slling 
8283912Slling static void
8295094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
8303912Slling {
8315094Slling 	zprop_list_t *pl = cbp->cb_proplist;
8323912Slling 	int i;
8333912Slling 	char *title;
8343912Slling 	size_t len;
8353912Slling 
8363912Slling 	cbp->cb_first = B_FALSE;
8373912Slling 	if (cbp->cb_scripted)
8383912Slling 		return;
8393912Slling 
8403912Slling 	/*
8413912Slling 	 * Start with the length of the column headers.
8423912Slling 	 */
8433912Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
8443912Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
8453912Slling 	    "PROPERTY"));
8463912Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
8473912Slling 	    "VALUE"));
84811022STom.Erickson@Sun.COM 	cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
84911022STom.Erickson@Sun.COM 	    "RECEIVED"));
8503912Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
8513912Slling 	    "SOURCE"));
8523912Slling 
8538269SMark.Musante@Sun.COM 	/* first property is always NAME */
8548269SMark.Musante@Sun.COM 	assert(cbp->cb_proplist->pl_prop ==
8558269SMark.Musante@Sun.COM 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
8568269SMark.Musante@Sun.COM 
8573912Slling 	/*
8583912Slling 	 * Go through and calculate the widths for each column.  For the
8593912Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
8603912Slling 	 * inheriting from the longest name.  This is acceptable because in the
8613912Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
8623912Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
86311022STom.Erickson@Sun.COM 	 * if the name of the property is much longer than any values we find.
8643912Slling 	 */
8653912Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
8663912Slling 		/*
8673912Slling 		 * 'PROPERTY' column
8683912Slling 		 */
8695094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
8705094Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
8715094Slling 			    zpool_prop_to_name(pl->pl_prop) :
8725094Slling 			    zfs_prop_to_name(pl->pl_prop);
8735094Slling 
8745094Slling 			len = strlen(propname);
8753912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8763912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8773912Slling 		} else {
8783912Slling 			len = strlen(pl->pl_user_prop);
8793912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8803912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8813912Slling 		}
8823912Slling 
8833912Slling 		/*
8848269SMark.Musante@Sun.COM 		 * 'VALUE' column.  The first property is always the 'name'
8858269SMark.Musante@Sun.COM 		 * property that was tacked on either by /sbin/zfs's
8868269SMark.Musante@Sun.COM 		 * zfs_do_get() or when calling zprop_expand_list(), so we
8878269SMark.Musante@Sun.COM 		 * ignore its width.  If the user specified the name property
8888269SMark.Musante@Sun.COM 		 * to display, then it will be later in the list in any case.
8893912Slling 		 */
8908269SMark.Musante@Sun.COM 		if (pl != cbp->cb_proplist &&
8913912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
8923912Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
8933912Slling 
89411022STom.Erickson@Sun.COM 		/* 'RECEIVED' column. */
89511022STom.Erickson@Sun.COM 		if (pl != cbp->cb_proplist &&
89611022STom.Erickson@Sun.COM 		    pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
89711022STom.Erickson@Sun.COM 			cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
89811022STom.Erickson@Sun.COM 
8993912Slling 		/*
9003912Slling 		 * 'NAME' and 'SOURCE' columns
9013912Slling 		 */
9025094Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
9035094Slling 		    ZFS_PROP_NAME) &&
9043912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
9053912Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
9063912Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
9073912Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
9083912Slling 		}
9093912Slling 	}
9103912Slling 
9113912Slling 	/*
9123912Slling 	 * Now go through and print the headers.
9133912Slling 	 */
91411022STom.Erickson@Sun.COM 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
9153912Slling 		switch (cbp->cb_columns[i]) {
9163912Slling 		case GET_COL_NAME:
9173912Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
9183912Slling 			break;
9193912Slling 		case GET_COL_PROPERTY:
9203912Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
9213912Slling 			break;
9223912Slling 		case GET_COL_VALUE:
9233912Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
9243912Slling 			break;
92511022STom.Erickson@Sun.COM 		case GET_COL_RECVD:
92611022STom.Erickson@Sun.COM 			title = dgettext(TEXT_DOMAIN, "RECEIVED");
92711022STom.Erickson@Sun.COM 			break;
9283912Slling 		case GET_COL_SOURCE:
9293912Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
9303912Slling 			break;
9313912Slling 		default:
9323912Slling 			title = NULL;
9333912Slling 		}
9343912Slling 
9353912Slling 		if (title != NULL) {
93611022STom.Erickson@Sun.COM 			if (i == (ZFS_GET_NCOLS - 1) ||
93711022STom.Erickson@Sun.COM 			    cbp->cb_columns[i + 1] == GET_COL_NONE)
9383912Slling 				(void) printf("%s", title);
9393912Slling 			else
9403912Slling 				(void) printf("%-*s  ",
9413912Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
9423912Slling 				    title);
9433912Slling 		}
9443912Slling 	}
9453912Slling 	(void) printf("\n");
9463912Slling }
9473912Slling 
9483912Slling /*
9493912Slling  * Display a single line of output, according to the settings in the callback
9503912Slling  * structure.
9513912Slling  */
9523912Slling void
9535094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
9545094Slling     const char *propname, const char *value, zprop_source_t sourcetype,
95511022STom.Erickson@Sun.COM     const char *source, const char *recvd_value)
9563912Slling {
9573912Slling 	int i;
9583912Slling 	const char *str;
9593912Slling 	char buf[128];
9603912Slling 
9613912Slling 	/*
9623912Slling 	 * Ignore those source types that the user has chosen to ignore.
9633912Slling 	 */
9643912Slling 	if ((sourcetype & cbp->cb_sources) == 0)
9653912Slling 		return;
9663912Slling 
9673912Slling 	if (cbp->cb_first)
9685094Slling 		zprop_print_headers(cbp, cbp->cb_type);
9693912Slling 
97011022STom.Erickson@Sun.COM 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
9713912Slling 		switch (cbp->cb_columns[i]) {
9723912Slling 		case GET_COL_NAME:
9733912Slling 			str = name;
9743912Slling 			break;
9753912Slling 
9763912Slling 		case GET_COL_PROPERTY:
9773912Slling 			str = propname;
9783912Slling 			break;
9793912Slling 
9803912Slling 		case GET_COL_VALUE:
9813912Slling 			str = value;
9823912Slling 			break;
9833912Slling 
9843912Slling 		case GET_COL_SOURCE:
9853912Slling 			switch (sourcetype) {
9865094Slling 			case ZPROP_SRC_NONE:
9873912Slling 				str = "-";
9883912Slling 				break;
9893912Slling 
9905094Slling 			case ZPROP_SRC_DEFAULT:
9913912Slling 				str = "default";
9923912Slling 				break;
9933912Slling 
9945094Slling 			case ZPROP_SRC_LOCAL:
9953912Slling 				str = "local";
9963912Slling 				break;
9973912Slling 
9985094Slling 			case ZPROP_SRC_TEMPORARY:
9993912Slling 				str = "temporary";
10003912Slling 				break;
10013912Slling 
10025094Slling 			case ZPROP_SRC_INHERITED:
10033912Slling 				(void) snprintf(buf, sizeof (buf),
10043912Slling 				    "inherited from %s", source);
10053912Slling 				str = buf;
10063912Slling 				break;
100711022STom.Erickson@Sun.COM 			case ZPROP_SRC_RECEIVED:
100811022STom.Erickson@Sun.COM 				str = "received";
100911022STom.Erickson@Sun.COM 				break;
10103912Slling 			}
10113912Slling 			break;
10123912Slling 
101311022STom.Erickson@Sun.COM 		case GET_COL_RECVD:
101411022STom.Erickson@Sun.COM 			str = (recvd_value == NULL ? "-" : recvd_value);
101511022STom.Erickson@Sun.COM 			break;
101611022STom.Erickson@Sun.COM 
10173912Slling 		default:
10183912Slling 			continue;
10193912Slling 		}
10203912Slling 
102111022STom.Erickson@Sun.COM 		if (cbp->cb_columns[i + 1] == GET_COL_NONE)
10223912Slling 			(void) printf("%s", str);
10233912Slling 		else if (cbp->cb_scripted)
10243912Slling 			(void) printf("%s\t", str);
10253912Slling 		else
10263912Slling 			(void) printf("%-*s  ",
10273912Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
10283912Slling 			    str);
10293912Slling 	}
10303912Slling 
10313912Slling 	(void) printf("\n");
10323912Slling }
10334543Smarks 
10345094Slling /*
10355094Slling  * Given a numeric suffix, convert the value into a number of bits that the
10365094Slling  * resulting value must be shifted.
10375094Slling  */
10385094Slling static int
10395094Slling str2shift(libzfs_handle_t *hdl, const char *buf)
10405094Slling {
10415094Slling 	const char *ends = "BKMGTPEZ";
10425094Slling 	int i;
10435094Slling 
10445094Slling 	if (buf[0] == '\0')
10455094Slling 		return (0);
10465094Slling 	for (i = 0; i < strlen(ends); i++) {
10475094Slling 		if (toupper(buf[0]) == ends[i])
10485094Slling 			break;
10495094Slling 	}
10505094Slling 	if (i == strlen(ends)) {
10515094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10525094Slling 		    "invalid numeric suffix '%s'"), buf);
10535094Slling 		return (-1);
10545094Slling 	}
10555094Slling 
10565094Slling 	/*
10575094Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
10585094Slling 	 * allow 'BB' - that's just weird.
10595094Slling 	 */
10605094Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
10615094Slling 	    toupper(buf[0]) != 'B'))
10625094Slling 		return (10*i);
10635094Slling 
10645094Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10655094Slling 	    "invalid numeric suffix '%s'"), buf);
10665094Slling 	return (-1);
10675094Slling }
10685094Slling 
10695094Slling /*
10705094Slling  * Convert a string of the form '100G' into a real number.  Used when setting
10715094Slling  * properties or creating a volume.  'buf' is used to place an extended error
10725094Slling  * message for the caller to use.
10735094Slling  */
10744543Smarks int
10755094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
10765094Slling {
10775094Slling 	char *end;
10785094Slling 	int shift;
10795094Slling 
10805094Slling 	*num = 0;
10815094Slling 
10825094Slling 	/* Check to see if this looks like a number.  */
10835094Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
10845094Slling 		if (hdl)
10855094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10865094Slling 			    "bad numeric value '%s'"), value);
10875094Slling 		return (-1);
10885094Slling 	}
10895094Slling 
109010817SEric.Schrock@Sun.COM 	/* Rely on strtoull() to process the numeric portion.  */
10915094Slling 	errno = 0;
10928343SEric.Schrock@Sun.COM 	*num = strtoull(value, &end, 10);
10935094Slling 
10945094Slling 	/*
10955094Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
10965094Slling 	 * in a 64-bit value.
10975094Slling 	 */
10985094Slling 	if (errno == ERANGE) {
10995094Slling 		if (hdl)
11005094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11015094Slling 			    "numeric value is too large"));
11025094Slling 		return (-1);
11035094Slling 	}
11045094Slling 
11055094Slling 	/*
11065094Slling 	 * If we have a decimal value, then do the computation with floating
11075094Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
11085094Slling 	 */
11095094Slling 	if (*end == '.') {
11105094Slling 		double fval = strtod(value, &end);
11115094Slling 
11125094Slling 		if ((shift = str2shift(hdl, end)) == -1)
11135094Slling 			return (-1);
11145094Slling 
11155094Slling 		fval *= pow(2, shift);
11165094Slling 
11175094Slling 		if (fval > UINT64_MAX) {
11185094Slling 			if (hdl)
11195094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11205094Slling 				    "numeric value is too large"));
11215094Slling 			return (-1);
11225094Slling 		}
11235094Slling 
11245094Slling 		*num = (uint64_t)fval;
11255094Slling 	} else {
11265094Slling 		if ((shift = str2shift(hdl, end)) == -1)
11275094Slling 			return (-1);
11285094Slling 
11295094Slling 		/* Check for overflow */
11305094Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
11315094Slling 			if (hdl)
11325094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11335094Slling 				    "numeric value is too large"));
11345094Slling 			return (-1);
11355094Slling 		}
11365094Slling 
11375094Slling 		*num <<= shift;
11385094Slling 	}
11395094Slling 
11405094Slling 	return (0);
11415094Slling }
11425094Slling 
11435094Slling /*
11445094Slling  * Given a propname=value nvpair to set, parse any numeric properties
11455094Slling  * (index, boolean, etc) if they are specified as strings and add the
11465094Slling  * resulting nvpair to the returned nvlist.
11475094Slling  *
11485094Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
11495094Slling  * We want the user to be able to ignore this fact and specify properties
11505094Slling  * as native values (numbers, for example) or as strings (to simplify
11515094Slling  * command line utilities).  This also handles converting index types
11525094Slling  * (compression, checksum, etc) from strings to their on-disk index.
11535094Slling  */
11545094Slling int
11555094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
11565094Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
11575094Slling     const char *errbuf)
11584543Smarks {
11595094Slling 	data_type_t datatype = nvpair_type(elem);
11605094Slling 	zprop_type_t proptype;
11615094Slling 	const char *propname;
11625094Slling 	char *value;
11635094Slling 	boolean_t isnone = B_FALSE;
11645094Slling 
11655094Slling 	if (type == ZFS_TYPE_POOL) {
11665094Slling 		proptype = zpool_prop_get_type(prop);
11675094Slling 		propname = zpool_prop_to_name(prop);
11685094Slling 	} else {
11695094Slling 		proptype = zfs_prop_get_type(prop);
11705094Slling 		propname = zfs_prop_to_name(prop);
11715094Slling 	}
11725094Slling 
11735094Slling 	/*
11745094Slling 	 * Convert any properties to the internal DSL value types.
11755094Slling 	 */
11765094Slling 	*svalp = NULL;
11775094Slling 	*ivalp = 0;
11785094Slling 
11795094Slling 	switch (proptype) {
11805094Slling 	case PROP_TYPE_STRING:
11815094Slling 		if (datatype != DATA_TYPE_STRING) {
11825094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11835094Slling 			    "'%s' must be a string"), nvpair_name(elem));
11845094Slling 			goto error;
11855094Slling 		}
11865094Slling 		(void) nvpair_value_string(elem, svalp);
11875094Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
11885094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11895094Slling 			    "'%s' is too long"), nvpair_name(elem));
11905094Slling 			goto error;
11915094Slling 		}
11925094Slling 		break;
11935094Slling 
11945094Slling 	case PROP_TYPE_NUMBER:
11955094Slling 		if (datatype == DATA_TYPE_STRING) {
11965094Slling 			(void) nvpair_value_string(elem, &value);
11975094Slling 			if (strcmp(value, "none") == 0) {
11985094Slling 				isnone = B_TRUE;
11995094Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
12005094Slling 			    != 0) {
12015094Slling 				goto error;
12025094Slling 			}
12035094Slling 		} else if (datatype == DATA_TYPE_UINT64) {
12045094Slling 			(void) nvpair_value_uint64(elem, ivalp);
12055094Slling 		} else {
12065094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12075094Slling 			    "'%s' must be a number"), nvpair_name(elem));
12085094Slling 			goto error;
12095094Slling 		}
12105094Slling 
12115094Slling 		/*
12125094Slling 		 * Quota special: force 'none' and don't allow 0.
12135094Slling 		 */
12145378Sck153898 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
12155378Sck153898 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
12165094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12175378Sck153898 			    "use 'none' to disable quota/refquota"));
12185094Slling 			goto error;
12195094Slling 		}
12205094Slling 		break;
12215094Slling 
12225094Slling 	case PROP_TYPE_INDEX:
12235378Sck153898 		if (datatype != DATA_TYPE_STRING) {
12245094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12255094Slling 			    "'%s' must be a string"), nvpair_name(elem));
12265094Slling 			goto error;
12275094Slling 		}
12285094Slling 
12295378Sck153898 		(void) nvpair_value_string(elem, &value);
12305378Sck153898 
12315094Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
12325094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12335094Slling 			    "'%s' must be one of '%s'"), propname,
12345094Slling 			    zprop_values(prop, type));
12355094Slling 			goto error;
12365094Slling 		}
12375094Slling 		break;
12385094Slling 
12395094Slling 	default:
12405094Slling 		abort();
12415094Slling 	}
12424543Smarks 
12435094Slling 	/*
12445094Slling 	 * Add the result to our return set of properties.
12455094Slling 	 */
12465094Slling 	if (*svalp != NULL) {
12475094Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
12485094Slling 			(void) no_memory(hdl);
12495094Slling 			return (-1);
12505094Slling 		}
12515094Slling 	} else {
12525094Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
12535094Slling 			(void) no_memory(hdl);
12545094Slling 			return (-1);
12555094Slling 		}
12565094Slling 	}
12575094Slling 
12585094Slling 	return (0);
12595094Slling error:
12605094Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12615094Slling 	return (-1);
12625094Slling }
12635094Slling 
12647390SMatthew.Ahrens@Sun.COM static int
12657390SMatthew.Ahrens@Sun.COM addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
12667390SMatthew.Ahrens@Sun.COM     zfs_type_t type)
12677390SMatthew.Ahrens@Sun.COM {
12687390SMatthew.Ahrens@Sun.COM 	int prop;
12697390SMatthew.Ahrens@Sun.COM 	zprop_list_t *entry;
12707390SMatthew.Ahrens@Sun.COM 
12717390SMatthew.Ahrens@Sun.COM 	prop = zprop_name_to_prop(propname, type);
12727390SMatthew.Ahrens@Sun.COM 
12737390SMatthew.Ahrens@Sun.COM 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
12747390SMatthew.Ahrens@Sun.COM 		prop = ZPROP_INVAL;
12757390SMatthew.Ahrens@Sun.COM 
12767390SMatthew.Ahrens@Sun.COM 	/*
12777390SMatthew.Ahrens@Sun.COM 	 * When no property table entry can be found, return failure if
12787390SMatthew.Ahrens@Sun.COM 	 * this is a pool property or if this isn't a user-defined
12797390SMatthew.Ahrens@Sun.COM 	 * dataset property,
12807390SMatthew.Ahrens@Sun.COM 	 */
12817390SMatthew.Ahrens@Sun.COM 	if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
12829396SMatthew.Ahrens@Sun.COM 	    (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) {
12837390SMatthew.Ahrens@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12847390SMatthew.Ahrens@Sun.COM 		    "invalid property '%s'"), propname);
12857390SMatthew.Ahrens@Sun.COM 		return (zfs_error(hdl, EZFS_BADPROP,
12867390SMatthew.Ahrens@Sun.COM 		    dgettext(TEXT_DOMAIN, "bad property list")));
12877390SMatthew.Ahrens@Sun.COM 	}
12887390SMatthew.Ahrens@Sun.COM 
12897390SMatthew.Ahrens@Sun.COM 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
12907390SMatthew.Ahrens@Sun.COM 		return (-1);
12917390SMatthew.Ahrens@Sun.COM 
12927390SMatthew.Ahrens@Sun.COM 	entry->pl_prop = prop;
12937390SMatthew.Ahrens@Sun.COM 	if (prop == ZPROP_INVAL) {
12947390SMatthew.Ahrens@Sun.COM 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) {
12957390SMatthew.Ahrens@Sun.COM 			free(entry);
12967390SMatthew.Ahrens@Sun.COM 			return (-1);
12977390SMatthew.Ahrens@Sun.COM 		}
12987390SMatthew.Ahrens@Sun.COM 		entry->pl_width = strlen(propname);
12997390SMatthew.Ahrens@Sun.COM 	} else {
13007390SMatthew.Ahrens@Sun.COM 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
13017390SMatthew.Ahrens@Sun.COM 		    type);
13027390SMatthew.Ahrens@Sun.COM 	}
13037390SMatthew.Ahrens@Sun.COM 
13047390SMatthew.Ahrens@Sun.COM 	*listp = entry;
13057390SMatthew.Ahrens@Sun.COM 
13067390SMatthew.Ahrens@Sun.COM 	return (0);
13077390SMatthew.Ahrens@Sun.COM }
13087390SMatthew.Ahrens@Sun.COM 
13095094Slling /*
13105094Slling  * Given a comma-separated list of properties, construct a property list
13115094Slling  * containing both user-defined and native properties.  This function will
13125094Slling  * return a NULL list if 'all' is specified, which can later be expanded
13135094Slling  * by zprop_expand_list().
13145094Slling  */
13155094Slling int
13165094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
13175094Slling     zfs_type_t type)
13185094Slling {
13195094Slling 	*listp = NULL;
13205094Slling 
13215094Slling 	/*
13225094Slling 	 * If 'all' is specified, return a NULL list.
13235094Slling 	 */
13245094Slling 	if (strcmp(props, "all") == 0)
13255094Slling 		return (0);
13265094Slling 
13275094Slling 	/*
13285094Slling 	 * If no props were specified, return an error.
13295094Slling 	 */
13305094Slling 	if (props[0] == '\0') {
13315094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13325094Slling 		    "no properties specified"));
13335094Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
13345094Slling 		    "bad property list")));
13354543Smarks 	}
13365094Slling 
13375094Slling 	/*
13385094Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
13395094Slling 	 * aliases makes this more effort than it's worth.
13405094Slling 	 */
13417390SMatthew.Ahrens@Sun.COM 	while (*props != '\0') {
13427390SMatthew.Ahrens@Sun.COM 		size_t len;
13437390SMatthew.Ahrens@Sun.COM 		char *p;
13447390SMatthew.Ahrens@Sun.COM 		char c;
13457390SMatthew.Ahrens@Sun.COM 
13467390SMatthew.Ahrens@Sun.COM 		if ((p = strchr(props, ',')) == NULL) {
13477390SMatthew.Ahrens@Sun.COM 			len = strlen(props);
13487390SMatthew.Ahrens@Sun.COM 			p = props + len;
13495094Slling 		} else {
13507390SMatthew.Ahrens@Sun.COM 			len = p - props;
13515094Slling 		}
13525094Slling 
13535094Slling 		/*
13545094Slling 		 * Check for empty options.
13555094Slling 		 */
13565094Slling 		if (len == 0) {
13575094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13585094Slling 			    "empty property name"));
13595094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
13605094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
13615094Slling 		}
13625094Slling 
13635094Slling 		/*
13645094Slling 		 * Check all regular property names.
13655094Slling 		 */
13667390SMatthew.Ahrens@Sun.COM 		c = props[len];
13677390SMatthew.Ahrens@Sun.COM 		props[len] = '\0';
13685094Slling 
13697390SMatthew.Ahrens@Sun.COM 		if (strcmp(props, "space") == 0) {
13707390SMatthew.Ahrens@Sun.COM 			static char *spaceprops[] = {
13717390SMatthew.Ahrens@Sun.COM 				"name", "avail", "used", "usedbysnapshots",
13727390SMatthew.Ahrens@Sun.COM 				"usedbydataset", "usedbyrefreservation",
13737390SMatthew.Ahrens@Sun.COM 				"usedbychildren", NULL
13747390SMatthew.Ahrens@Sun.COM 			};
13757390SMatthew.Ahrens@Sun.COM 			int i;
13767390SMatthew.Ahrens@Sun.COM 
13777390SMatthew.Ahrens@Sun.COM 			for (i = 0; spaceprops[i]; i++) {
13787390SMatthew.Ahrens@Sun.COM 				if (addlist(hdl, spaceprops[i], listp, type))
13797390SMatthew.Ahrens@Sun.COM 					return (-1);
13807390SMatthew.Ahrens@Sun.COM 				listp = &(*listp)->pl_next;
13817390SMatthew.Ahrens@Sun.COM 			}
13827390SMatthew.Ahrens@Sun.COM 		} else {
13837390SMatthew.Ahrens@Sun.COM 			if (addlist(hdl, props, listp, type))
13847390SMatthew.Ahrens@Sun.COM 				return (-1);
13857390SMatthew.Ahrens@Sun.COM 			listp = &(*listp)->pl_next;
13865094Slling 		}
13875094Slling 
13887390SMatthew.Ahrens@Sun.COM 		props = p;
13895094Slling 		if (c == ',')
13907390SMatthew.Ahrens@Sun.COM 			props++;
13915094Slling 	}
13925094Slling 
13935094Slling 	return (0);
13945094Slling }
13955094Slling 
13965094Slling void
13975094Slling zprop_free_list(zprop_list_t *pl)
13985094Slling {
13995094Slling 	zprop_list_t *next;
14004543Smarks 
14015094Slling 	while (pl != NULL) {
14025094Slling 		next = pl->pl_next;
14035094Slling 		free(pl->pl_user_prop);
14045094Slling 		free(pl);
14055094Slling 		pl = next;
14065094Slling 	}
14075094Slling }
14085094Slling 
14095094Slling typedef struct expand_data {
14105094Slling 	zprop_list_t	**last;
14115094Slling 	libzfs_handle_t	*hdl;
14125094Slling 	zfs_type_t type;
14135094Slling } expand_data_t;
14145094Slling 
14155094Slling int
14165094Slling zprop_expand_list_cb(int prop, void *cb)
14175094Slling {
14185094Slling 	zprop_list_t *entry;
14195094Slling 	expand_data_t *edp = cb;
14205094Slling 
14215094Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
14225094Slling 		return (ZPROP_INVAL);
14235094Slling 
14245094Slling 	entry->pl_prop = prop;
14255094Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
14265094Slling 	entry->pl_all = B_TRUE;
14275094Slling 
14285094Slling 	*(edp->last) = entry;
14295094Slling 	edp->last = &entry->pl_next;
14305094Slling 
14315094Slling 	return (ZPROP_CONT);
14324543Smarks }
14335094Slling 
14345094Slling int
14355094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
14365094Slling {
14375094Slling 	zprop_list_t *entry;
14385094Slling 	zprop_list_t **last;
14395094Slling 	expand_data_t exp;
14405094Slling 
14415094Slling 	if (*plp == NULL) {
14425094Slling 		/*
14435094Slling 		 * If this is the very first time we've been called for an 'all'
14445094Slling 		 * specification, expand the list to include all native
14455094Slling 		 * properties.
14465094Slling 		 */
14475094Slling 		last = plp;
14485094Slling 
14495094Slling 		exp.last = last;
14505094Slling 		exp.hdl = hdl;
14515094Slling 		exp.type = type;
14525094Slling 
14535094Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
14545094Slling 		    B_FALSE, type) == ZPROP_INVAL)
14555094Slling 			return (-1);
14565094Slling 
14575094Slling 		/*
14585094Slling 		 * Add 'name' to the beginning of the list, which is handled
14595094Slling 		 * specially.
14605094Slling 		 */
14615094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
14625094Slling 			return (-1);
14635094Slling 
14645094Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
14655094Slling 		    ZFS_PROP_NAME;
14665094Slling 		entry->pl_width = zprop_width(entry->pl_prop,
14675094Slling 		    &entry->pl_fixed, type);
14685094Slling 		entry->pl_all = B_TRUE;
14695094Slling 		entry->pl_next = *plp;
14705094Slling 		*plp = entry;
14715094Slling 	}
14725094Slling 	return (0);
14735094Slling }
14745094Slling 
14755094Slling int
14765094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
14775094Slling     zfs_type_t type)
14785094Slling {
14795094Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
14805094Slling }
1481