xref: /onnv-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision 13049:bda0decf867b)
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
libzfs_errno(libzfs_handle_t * hdl)492082Seschrock libzfs_errno(libzfs_handle_t *hdl)
502082Seschrock {
512082Seschrock 	return (hdl->libzfs_error);
522082Seschrock }
53789Sahrens 
542082Seschrock const char *
libzfs_error_action(libzfs_handle_t * hdl)552082Seschrock libzfs_error_action(libzfs_handle_t *hdl)
562082Seschrock {
572082Seschrock 	return (hdl->libzfs_action);
582082Seschrock }
592082Seschrock 
602082Seschrock const char *
libzfs_error_description(libzfs_handle_t * hdl)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:
72*13049SGeorge.Wilson@Sun.COM 		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:
92*13049SGeorge.Wilson@Sun.COM 		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"));
1845363Seschrock 	case EZFS_BADCACHE:
1855363Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
1865450Sbrendan 	case EZFS_ISL2CACHE:
1875450Sbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
1886423Sgw25295 	case EZFS_VDEVNOTSUP:
1896423Sgw25295 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
1906423Sgw25295 		    "supported"));
1917042Sgw25295 	case EZFS_NOTSUP:
1927042Sgw25295 		return (dgettext(TEXT_DOMAIN, "operation not supported "
1937042Sgw25295 		    "on this dataset"));
1947214Slling 	case EZFS_ACTIVE_SPARE:
1957214Slling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
1967214Slling 		    "device"));
1979701SGeorge.Wilson@Sun.COM 	case EZFS_UNPLAYED_LOGS:
1989701SGeorge.Wilson@Sun.COM 		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
1999701SGeorge.Wilson@Sun.COM 		    "logs"));
20010242Schris.kirby@sun.com 	case EZFS_REFTAG_RELE:
20110242Schris.kirby@sun.com 		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
20210242Schris.kirby@sun.com 	case EZFS_REFTAG_HOLD:
20310242Schris.kirby@sun.com 		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
20410242Schris.kirby@sun.com 		    "dataset"));
20510342Schris.kirby@sun.com 	case EZFS_TAGTOOLONG:
20610342Schris.kirby@sun.com 		return (dgettext(TEXT_DOMAIN, "tag too long"));
20711007SLori.Alt@Sun.COM 	case EZFS_PIPEFAILED:
20811007SLori.Alt@Sun.COM 		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
20911007SLori.Alt@Sun.COM 	case EZFS_THREADCREATEFAILED:
21011007SLori.Alt@Sun.COM 		return (dgettext(TEXT_DOMAIN, "thread create failed"));
21111422SMark.Musante@Sun.COM 	case EZFS_POSTSPLIT_ONLINE:
21211422SMark.Musante@Sun.COM 		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
21311422SMark.Musante@Sun.COM 		    "into a new one"));
21412296SLin.Ling@Sun.COM 	case EZFS_SCRUBBING:
21512296SLin.Ling@Sun.COM 		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
21612296SLin.Ling@Sun.COM 		    "use 'zpool scrub -s' to cancel current scrub"));
21712296SLin.Ling@Sun.COM 	case EZFS_NO_SCRUB:
21812296SLin.Ling@Sun.COM 		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
21913043STim.Haley@Sun.COM 	case EZFS_DIFF:
22013043STim.Haley@Sun.COM 		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
22113043STim.Haley@Sun.COM 	case EZFS_DIFFDATA:
22213043STim.Haley@Sun.COM 		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
223*13049SGeorge.Wilson@Sun.COM 	case EZFS_POOLREADONLY:
224*13049SGeorge.Wilson@Sun.COM 		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
2252082Seschrock 	case EZFS_UNKNOWN:
2262082Seschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
2272082Seschrock 	default:
2282500Seschrock 		assert(hdl->libzfs_error == 0);
2292500Seschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
2302082Seschrock 	}
2312082Seschrock }
2322082Seschrock 
2332082Seschrock /*PRINTFLIKE2*/
234789Sahrens void
zfs_error_aux(libzfs_handle_t * hdl,const char * fmt,...)2352082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
236789Sahrens {
237789Sahrens 	va_list ap;
238789Sahrens 
239789Sahrens 	va_start(ap, fmt);
240789Sahrens 
2412082Seschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
2422082Seschrock 	    fmt, ap);
2432082Seschrock 	hdl->libzfs_desc_active = 1;
244789Sahrens 
245789Sahrens 	va_end(ap);
246789Sahrens }
247789Sahrens 
2482082Seschrock static void
zfs_verror(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)2492082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
2502082Seschrock {
2512082Seschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
2522082Seschrock 	    fmt, ap);
2532082Seschrock 	hdl->libzfs_error = error;
2542082Seschrock 
2552082Seschrock 	if (hdl->libzfs_desc_active)
2562082Seschrock 		hdl->libzfs_desc_active = 0;
2572082Seschrock 	else
2582082Seschrock 		hdl->libzfs_desc[0] = '\0';
2592082Seschrock 
2602082Seschrock 	if (hdl->libzfs_printerr) {
2612082Seschrock 		if (error == EZFS_UNKNOWN) {
2622082Seschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
2632082Seschrock 			    "error: %s\n"), libzfs_error_description(hdl));
2642082Seschrock 			abort();
2652082Seschrock 		}
2662082Seschrock 
2672082Seschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
2683912Slling 		    libzfs_error_description(hdl));
2692082Seschrock 		if (error == EZFS_NOMEM)
2702082Seschrock 			exit(1);
2712082Seschrock 	}
2722082Seschrock }
2732082Seschrock 
2743237Slling int
zfs_error(libzfs_handle_t * hdl,int error,const char * msg)2753237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
2763237Slling {
2773237Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
2783237Slling }
2793237Slling 
2802082Seschrock /*PRINTFLIKE3*/
2812082Seschrock int
zfs_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)2823237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
2832082Seschrock {
2842082Seschrock 	va_list ap;
2852082Seschrock 
2862082Seschrock 	va_start(ap, fmt);
2872082Seschrock 
2882082Seschrock 	zfs_verror(hdl, error, fmt, ap);
2892082Seschrock 
2902082Seschrock 	va_end(ap);
2912082Seschrock 
2922082Seschrock 	return (-1);
2932082Seschrock }
2942082Seschrock 
2952082Seschrock static int
zfs_common_error(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)2962082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
2972082Seschrock     va_list ap)
2982082Seschrock {
2992082Seschrock 	switch (error) {
3002082Seschrock 	case EPERM:
3012082Seschrock 	case EACCES:
3022082Seschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
3032082Seschrock 		return (-1);
3042082Seschrock 
3054543Smarks 	case ECANCELED:
3064543Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
3074543Smarks 		return (-1);
3084543Smarks 
3092082Seschrock 	case EIO:
3102082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3112082Seschrock 		return (-1);
3122082Seschrock 
31311807SSam.Falkner@Sun.COM 	case EFAULT:
31411807SSam.Falkner@Sun.COM 		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
31511807SSam.Falkner@Sun.COM 		return (-1);
31611807SSam.Falkner@Sun.COM 
3172082Seschrock 	case EINTR:
3182082Seschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
3192082Seschrock 		return (-1);
3202082Seschrock 	}
3212082Seschrock 
3222082Seschrock 	return (0);
3232082Seschrock }
3242082Seschrock 
3253237Slling int
zfs_standard_error(libzfs_handle_t * hdl,int error,const char * msg)3263237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3273237Slling {
3283237Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
3293237Slling }
3303237Slling 
3312082Seschrock /*PRINTFLIKE3*/
3322082Seschrock int
zfs_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)3333237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
334789Sahrens {
335789Sahrens 	va_list ap;
336789Sahrens 
337789Sahrens 	va_start(ap, fmt);
338789Sahrens 
3392082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3402082Seschrock 		va_end(ap);
3412082Seschrock 		return (-1);
3422082Seschrock 	}
3432082Seschrock 
3442082Seschrock 	switch (error) {
3452082Seschrock 	case ENXIO:
3465807Sck153898 	case ENODEV:
3472082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3482082Seschrock 		break;
3492082Seschrock 
3502082Seschrock 	case ENOENT:
3512082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3522082Seschrock 		    "dataset does not exist"));
3532082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3542082Seschrock 		break;
3552082Seschrock 
3562082Seschrock 	case ENOSPC:
3572082Seschrock 	case EDQUOT:
3582082Seschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
3592082Seschrock 		return (-1);
3602082Seschrock 
3612082Seschrock 	case EEXIST:
3622082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3632082Seschrock 		    "dataset already exists"));
3642082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3652082Seschrock 		break;
3662082Seschrock 
3672082Seschrock 	case EBUSY:
3682082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3692082Seschrock 		    "dataset is busy"));
3702082Seschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
3712082Seschrock 		break;
3724543Smarks 	case EROFS:
373*13049SGeorge.Wilson@Sun.COM 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
3744543Smarks 		break;
3753978Smmusante 	case ENAMETOOLONG:
3763978Smmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
3773978Smmusante 		break;
3785860Sck153898 	case ENOTSUP:
3795860Sck153898 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
3805860Sck153898 		break;
3819234SGeorge.Wilson@Sun.COM 	case EAGAIN:
3829234SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3839234SGeorge.Wilson@Sun.COM 		    "pool I/O is currently suspended"));
3849234SGeorge.Wilson@Sun.COM 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
3859234SGeorge.Wilson@Sun.COM 		break;
3862082Seschrock 	default:
38711022STom.Erickson@Sun.COM 		zfs_error_aux(hdl, strerror(error));
3882082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
3892082Seschrock 		break;
390789Sahrens 	}
391789Sahrens 
392789Sahrens 	va_end(ap);
3932082Seschrock 	return (-1);
394789Sahrens }
395789Sahrens 
3963237Slling int
zpool_standard_error(libzfs_handle_t * hdl,int error,const char * msg)3973237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3983237Slling {
3993237Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
4003237Slling }
4013237Slling 
4022082Seschrock /*PRINTFLIKE3*/
4032082Seschrock int
zpool_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)4043237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
405789Sahrens {
4062082Seschrock 	va_list ap;
4072082Seschrock 
4082082Seschrock 	va_start(ap, fmt);
4092082Seschrock 
4102082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
4112082Seschrock 		va_end(ap);
4122082Seschrock 		return (-1);
4132082Seschrock 	}
4142082Seschrock 
4152082Seschrock 	switch (error) {
4162082Seschrock 	case ENODEV:
4172082Seschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
4182082Seschrock 		break;
4192082Seschrock 
4202082Seschrock 	case ENOENT:
4213912Slling 		zfs_error_aux(hdl,
4223912Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
4232082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
4242082Seschrock 		break;
4252082Seschrock 
4262082Seschrock 	case EEXIST:
4272082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4282082Seschrock 		    "pool already exists"));
4292082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
4302082Seschrock 		break;
4312082Seschrock 
4322082Seschrock 	case EBUSY:
4332082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
4347341SEric.Schrock@Sun.COM 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
4352082Seschrock 		break;
4362082Seschrock 
4372082Seschrock 	case ENXIO:
4382082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4392082Seschrock 		    "one or more devices is currently unavailable"));
4402082Seschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
4412082Seschrock 		break;
4422082Seschrock 
4432082Seschrock 	case ENAMETOOLONG:
4442082Seschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
4452082Seschrock 		break;
4462082Seschrock 
4473912Slling 	case ENOTSUP:
4483912Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
4493912Slling 		break;
4503912Slling 
4513912Slling 	case EINVAL:
4523912Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
4533912Slling 		break;
4543912Slling 
4554543Smarks 	case ENOSPC:
4564543Smarks 	case EDQUOT:
4574543Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
4584543Smarks 		return (-1);
459*13049SGeorge.Wilson@Sun.COM 
4609234SGeorge.Wilson@Sun.COM 	case EAGAIN:
4619234SGeorge.Wilson@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4629234SGeorge.Wilson@Sun.COM 		    "pool I/O is currently suspended"));
4639234SGeorge.Wilson@Sun.COM 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
4649234SGeorge.Wilson@Sun.COM 		break;
4654543Smarks 
466*13049SGeorge.Wilson@Sun.COM 	case EROFS:
467*13049SGeorge.Wilson@Sun.COM 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
468*13049SGeorge.Wilson@Sun.COM 		break;
469*13049SGeorge.Wilson@Sun.COM 
4702082Seschrock 	default:
4712082Seschrock 		zfs_error_aux(hdl, strerror(error));
4722082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
4732082Seschrock 	}
4742082Seschrock 
4752082Seschrock 	va_end(ap);
4762082Seschrock 	return (-1);
477789Sahrens }
478789Sahrens 
479789Sahrens /*
480789Sahrens  * Display an out of memory error message and abort the current program.
481789Sahrens  */
4822082Seschrock int
no_memory(libzfs_handle_t * hdl)4832082Seschrock no_memory(libzfs_handle_t *hdl)
484789Sahrens {
4852082Seschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
486789Sahrens }
487789Sahrens 
488789Sahrens /*
489789Sahrens  * A safe form of malloc() which will die if the allocation fails.
490789Sahrens  */
491789Sahrens void *
zfs_alloc(libzfs_handle_t * hdl,size_t size)4922082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
493789Sahrens {
494789Sahrens 	void *data;
495789Sahrens 
496789Sahrens 	if ((data = calloc(1, size)) == NULL)
4972082Seschrock 		(void) no_memory(hdl);
498789Sahrens 
499789Sahrens 	return (data);
500789Sahrens }
501789Sahrens 
502789Sahrens /*
50313043STim.Haley@Sun.COM  * A safe form of asprintf() which will die if the allocation fails.
50413043STim.Haley@Sun.COM  */
50513043STim.Haley@Sun.COM /*PRINTFLIKE2*/
50613043STim.Haley@Sun.COM char *
zfs_asprintf(libzfs_handle_t * hdl,const char * fmt,...)50713043STim.Haley@Sun.COM zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
50813043STim.Haley@Sun.COM {
50913043STim.Haley@Sun.COM 	va_list ap;
51013043STim.Haley@Sun.COM 	char *ret;
51113043STim.Haley@Sun.COM 	int err;
51213043STim.Haley@Sun.COM 
51313043STim.Haley@Sun.COM 	va_start(ap, fmt);
51413043STim.Haley@Sun.COM 
51513043STim.Haley@Sun.COM 	err = vasprintf(&ret, fmt, ap);
51613043STim.Haley@Sun.COM 
51713043STim.Haley@Sun.COM 	va_end(ap);
51813043STim.Haley@Sun.COM 
51913043STim.Haley@Sun.COM 	if (err < 0)
52013043STim.Haley@Sun.COM 		(void) no_memory(hdl);
52113043STim.Haley@Sun.COM 
52213043STim.Haley@Sun.COM 	return (ret);
52313043STim.Haley@Sun.COM }
52413043STim.Haley@Sun.COM 
52513043STim.Haley@Sun.COM /*
5262676Seschrock  * A safe form of realloc(), which also zeroes newly allocated space.
5272676Seschrock  */
5282676Seschrock void *
zfs_realloc(libzfs_handle_t * hdl,void * ptr,size_t oldsize,size_t newsize)5292676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
5302676Seschrock {
5312676Seschrock 	void *ret;
5322676Seschrock 
5332676Seschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
5342676Seschrock 		(void) no_memory(hdl);
5352676Seschrock 		return (NULL);
5362676Seschrock 	}
5372676Seschrock 
5382676Seschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
5392676Seschrock 	return (ret);
5402676Seschrock }
5412676Seschrock 
5422676Seschrock /*
543789Sahrens  * A safe form of strdup() which will die if the allocation fails.
544789Sahrens  */
545789Sahrens char *
zfs_strdup(libzfs_handle_t * hdl,const char * str)5462082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
547789Sahrens {
548789Sahrens 	char *ret;
549789Sahrens 
550789Sahrens 	if ((ret = strdup(str)) == NULL)
5512082Seschrock 		(void) no_memory(hdl);
552789Sahrens 
553789Sahrens 	return (ret);
554789Sahrens }
555789Sahrens 
556789Sahrens /*
557789Sahrens  * Convert a number to an appropriately human-readable output.
558789Sahrens  */
559789Sahrens void
zfs_nicenum(uint64_t num,char * buf,size_t buflen)560789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
561789Sahrens {
562789Sahrens 	uint64_t n = num;
563789Sahrens 	int index = 0;
564789Sahrens 	char u;
565789Sahrens 
566789Sahrens 	while (n >= 1024) {
5671162Seschrock 		n /= 1024;
568789Sahrens 		index++;
569789Sahrens 	}
570789Sahrens 
571789Sahrens 	u = " KMGTPE"[index];
572789Sahrens 
5731162Seschrock 	if (index == 0) {
574789Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5751162Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5761162Seschrock 		/*
5771162Seschrock 		 * If this is an even multiple of the base, always display
5781162Seschrock 		 * without any decimal precision.
5791162Seschrock 		 */
580789Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5811162Seschrock 	} else {
5821162Seschrock 		/*
5831162Seschrock 		 * We want to choose a precision that reflects the best choice
5841162Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5851162Seschrock 		 * we have numbers that are very close to an order of magnitude.
5861162Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5871162Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5881162Seschrock 		 * develop some complex heuristics for this, but it's much
5891162Seschrock 		 * easier just to try each combination in turn.
5901162Seschrock 		 */
5911162Seschrock 		int i;
5921162Seschrock 		for (i = 2; i >= 0; i--) {
5934451Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5944451Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5951162Seschrock 				break;
5961162Seschrock 		}
5971162Seschrock 	}
598789Sahrens }
5992082Seschrock 
6002082Seschrock void
libzfs_print_on_error(libzfs_handle_t * hdl,boolean_t printerr)6012082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
6022082Seschrock {
6032082Seschrock 	hdl->libzfs_printerr = printerr;
6042082Seschrock }
6052082Seschrock 
6062082Seschrock libzfs_handle_t *
libzfs_init(void)6072082Seschrock libzfs_init(void)
6082082Seschrock {
6092082Seschrock 	libzfs_handle_t *hdl;
6102082Seschrock 
61113043STim.Haley@Sun.COM 	if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
6122082Seschrock 		return (NULL);
6132082Seschrock 	}
6142082Seschrock 
6152500Seschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
6162082Seschrock 		free(hdl);
6172082Seschrock 		return (NULL);
6182082Seschrock 	}
6192082Seschrock 
6202082Seschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
6212082Seschrock 		(void) close(hdl->libzfs_fd);
6222082Seschrock 		free(hdl);
6232082Seschrock 		return (NULL);
6242082Seschrock 	}
6252082Seschrock 
6262082Seschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
6272082Seschrock 
6284787Sahrens 	zfs_prop_init();
6295094Slling 	zpool_prop_init();
6308811SEric.Taylor@Sun.COM 	libzfs_mnttab_init(hdl);
6314787Sahrens 
6322082Seschrock 	return (hdl);
6332082Seschrock }
6342082Seschrock 
6352082Seschrock void
libzfs_fini(libzfs_handle_t * hdl)6362082Seschrock libzfs_fini(libzfs_handle_t *hdl)
6372082Seschrock {
6382082Seschrock 	(void) close(hdl->libzfs_fd);
6392082Seschrock 	if (hdl->libzfs_mnttab)
6402082Seschrock 		(void) fclose(hdl->libzfs_mnttab);
6412082Seschrock 	if (hdl->libzfs_sharetab)
6422082Seschrock 		(void) fclose(hdl->libzfs_sharetab);
6434180Sdougm 	zfs_uninit_libshare(hdl);
6444543Smarks 	if (hdl->libzfs_log_str)
6454543Smarks 		(void) free(hdl->libzfs_log_str);
6466865Srm160521 	zpool_free_handles(hdl);
64710817SEric.Schrock@Sun.COM 	libzfs_fru_clear(hdl, B_TRUE);
6482082Seschrock 	namespace_clear(hdl);
6498811SEric.Taylor@Sun.COM 	libzfs_mnttab_fini(hdl);
6502082Seschrock 	free(hdl);
6512082Seschrock }
6522082Seschrock 
6532082Seschrock libzfs_handle_t *
zpool_get_handle(zpool_handle_t * zhp)6542082Seschrock zpool_get_handle(zpool_handle_t *zhp)
6552082Seschrock {
6562082Seschrock 	return (zhp->zpool_hdl);
6572082Seschrock }
6582082Seschrock 
6592082Seschrock libzfs_handle_t *
zfs_get_handle(zfs_handle_t * zhp)6602082Seschrock zfs_get_handle(zfs_handle_t *zhp)
6612082Seschrock {
6622082Seschrock 	return (zhp->zfs_hdl);
6632082Seschrock }
6642676Seschrock 
6657538SRichard.Morris@Sun.COM zpool_handle_t *
zfs_get_pool_handle(const zfs_handle_t * zhp)6667538SRichard.Morris@Sun.COM zfs_get_pool_handle(const zfs_handle_t *zhp)
6677538SRichard.Morris@Sun.COM {
6687538SRichard.Morris@Sun.COM 	return (zhp->zpool_hdl);
6697538SRichard.Morris@Sun.COM }
6707538SRichard.Morris@Sun.COM 
6712676Seschrock /*
6723635Sck153898  * Given a name, determine whether or not it's a valid path
6733635Sck153898  * (starts with '/' or "./").  If so, walk the mnttab trying
6743635Sck153898  * to match the device number.  If not, treat the path as an
6753635Sck153898  * fs/vol/snap name.
6763635Sck153898  */
6773635Sck153898 zfs_handle_t *
zfs_path_to_zhandle(libzfs_handle_t * hdl,char * path,zfs_type_t argtype)6783635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6793635Sck153898 {
6803635Sck153898 	struct stat64 statbuf;
6813635Sck153898 	struct extmnttab entry;
6823635Sck153898 	int ret;
6833635Sck153898 
6843635Sck153898 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6853635Sck153898 		/*
6863635Sck153898 		 * It's not a valid path, assume it's a name of type 'argtype'.
6873635Sck153898 		 */
6883635Sck153898 		return (zfs_open(hdl, path, argtype));
6893635Sck153898 	}
6903635Sck153898 
6913635Sck153898 	if (stat64(path, &statbuf) != 0) {
6923635Sck153898 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6933635Sck153898 		return (NULL);
6943635Sck153898 	}
6953635Sck153898 
6963635Sck153898 	rewind(hdl->libzfs_mnttab);
6973635Sck153898 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6983635Sck153898 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6993635Sck153898 		    statbuf.st_dev) {
7003635Sck153898 			break;
7013635Sck153898 		}
7023635Sck153898 	}
7033635Sck153898 	if (ret != 0) {
7043635Sck153898 		return (NULL);
7053635Sck153898 	}
7063635Sck153898 
7073635Sck153898 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7083635Sck153898 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
7093635Sck153898 		    path);
7103635Sck153898 		return (NULL);
7113635Sck153898 	}
7123635Sck153898 
7133635Sck153898 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
7143635Sck153898 }
7153635Sck153898 
7163635Sck153898 /*
7172676Seschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
7182676Seschrock  * an ioctl().
7192676Seschrock  */
7202676Seschrock int
zcmd_alloc_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,size_t len)7212676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
7222676Seschrock {
7232676Seschrock 	if (len == 0)
72412949SGeorge.Wilson@Sun.COM 		len = 16 * 1024;
7252676Seschrock 	zc->zc_nvlist_dst_size = len;
7262676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
7272676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
7282676Seschrock 		return (-1);
7292676Seschrock 
7302676Seschrock 	return (0);
7312676Seschrock }
7322676Seschrock 
7332676Seschrock /*
7342676Seschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
7352676Seschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
7362676Seschrock  * filled in by the kernel to indicate the actual required size.
7372676Seschrock  */
7382676Seschrock int
zcmd_expand_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc)7392676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
7402676Seschrock {
7412676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
7422676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
7432676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
7442676Seschrock 	    == NULL)
7452676Seschrock 		return (-1);
7462676Seschrock 
7472676Seschrock 	return (0);
7482676Seschrock }
7492676Seschrock 
7502676Seschrock /*
7512885Sahrens  * Called to free the src and dst nvlists stored in the command structure.
7522676Seschrock  */
7532676Seschrock void
zcmd_free_nvlists(zfs_cmd_t * zc)7542676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc)
7552676Seschrock {
7565094Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
7572676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
7582676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
7592676Seschrock }
7602676Seschrock 
7615094Slling static int
zcmd_write_nvlist_com(libzfs_handle_t * hdl,uint64_t * outnv,uint64_t * outlen,nvlist_t * nvl)7625094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
7635094Slling     nvlist_t *nvl)
7642676Seschrock {
7652676Seschrock 	char *packed;
7662676Seschrock 	size_t len;
7672676Seschrock 
7682676Seschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
7692676Seschrock 
7702676Seschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
7712676Seschrock 		return (-1);
7722676Seschrock 
7732676Seschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
7742676Seschrock 
7755094Slling 	*outnv = (uint64_t)(uintptr_t)packed;
7765094Slling 	*outlen = len;
7775094Slling 
7785094Slling 	return (0);
7795094Slling }
7802676Seschrock 
7815094Slling int
zcmd_write_conf_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t * nvl)7825094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7835094Slling {
7845094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
7855094Slling 	    &zc->zc_nvlist_conf_size, nvl));
7865094Slling }
7875094Slling 
7885094Slling int
zcmd_write_src_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t * nvl)7895094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7905094Slling {
7915094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
7925094Slling 	    &zc->zc_nvlist_src_size, nvl));
7932676Seschrock }
7942676Seschrock 
7952676Seschrock /*
7962676Seschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
7972676Seschrock  */
7982676Seschrock int
zcmd_read_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t ** nvlp)7992676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
8002676Seschrock {
8012676Seschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
8022676Seschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
8032676Seschrock 		return (no_memory(hdl));
8042676Seschrock 
8052676Seschrock 	return (0);
8062676Seschrock }
8073912Slling 
8085094Slling int
zfs_ioctl(libzfs_handle_t * hdl,int request,zfs_cmd_t * zc)8095094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
8105094Slling {
8115094Slling 	int error;
8125094Slling 
8135094Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
8145094Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
8155094Slling 	if (hdl->libzfs_log_str) {
8165094Slling 		free(hdl->libzfs_log_str);
8175094Slling 		hdl->libzfs_log_str = NULL;
8185094Slling 	}
8195094Slling 	zc->zc_history = 0;
8205094Slling 
8215094Slling 	return (error);
8225094Slling }
8235094Slling 
8245094Slling /*
8255094Slling  * ================================================================
8265094Slling  * API shared by zfs and zpool property management
8275094Slling  * ================================================================
8285094Slling  */
8295094Slling 
8303912Slling static void
zprop_print_headers(zprop_get_cbdata_t * cbp,zfs_type_t type)8315094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
8323912Slling {
8335094Slling 	zprop_list_t *pl = cbp->cb_proplist;
8343912Slling 	int i;
8353912Slling 	char *title;
8363912Slling 	size_t len;
8373912Slling 
8383912Slling 	cbp->cb_first = B_FALSE;
8393912Slling 	if (cbp->cb_scripted)
8403912Slling 		return;
8413912Slling 
8423912Slling 	/*
8433912Slling 	 * Start with the length of the column headers.
8443912Slling 	 */
8453912Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
8463912Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
8473912Slling 	    "PROPERTY"));
8483912Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
8493912Slling 	    "VALUE"));
85011022STom.Erickson@Sun.COM 	cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
85111022STom.Erickson@Sun.COM 	    "RECEIVED"));
8523912Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
8533912Slling 	    "SOURCE"));
8543912Slling 
8558269SMark.Musante@Sun.COM 	/* first property is always NAME */
8568269SMark.Musante@Sun.COM 	assert(cbp->cb_proplist->pl_prop ==
8578269SMark.Musante@Sun.COM 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
8588269SMark.Musante@Sun.COM 
8593912Slling 	/*
8603912Slling 	 * Go through and calculate the widths for each column.  For the
8613912Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
8623912Slling 	 * inheriting from the longest name.  This is acceptable because in the
8633912Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
8643912Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
86511022STom.Erickson@Sun.COM 	 * if the name of the property is much longer than any values we find.
8663912Slling 	 */
8673912Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
8683912Slling 		/*
8693912Slling 		 * 'PROPERTY' column
8703912Slling 		 */
8715094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
8725094Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
8735094Slling 			    zpool_prop_to_name(pl->pl_prop) :
8745094Slling 			    zfs_prop_to_name(pl->pl_prop);
8755094Slling 
8765094Slling 			len = strlen(propname);
8773912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8783912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8793912Slling 		} else {
8803912Slling 			len = strlen(pl->pl_user_prop);
8813912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8823912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8833912Slling 		}
8843912Slling 
8853912Slling 		/*
8868269SMark.Musante@Sun.COM 		 * 'VALUE' column.  The first property is always the 'name'
8878269SMark.Musante@Sun.COM 		 * property that was tacked on either by /sbin/zfs's
8888269SMark.Musante@Sun.COM 		 * zfs_do_get() or when calling zprop_expand_list(), so we
8898269SMark.Musante@Sun.COM 		 * ignore its width.  If the user specified the name property
8908269SMark.Musante@Sun.COM 		 * to display, then it will be later in the list in any case.
8913912Slling 		 */
8928269SMark.Musante@Sun.COM 		if (pl != cbp->cb_proplist &&
8933912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
8943912Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
8953912Slling 
89611022STom.Erickson@Sun.COM 		/* 'RECEIVED' column. */
89711022STom.Erickson@Sun.COM 		if (pl != cbp->cb_proplist &&
89811022STom.Erickson@Sun.COM 		    pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
89911022STom.Erickson@Sun.COM 			cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
90011022STom.Erickson@Sun.COM 
9013912Slling 		/*
9023912Slling 		 * 'NAME' and 'SOURCE' columns
9033912Slling 		 */
9045094Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
9055094Slling 		    ZFS_PROP_NAME) &&
9063912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
9073912Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
9083912Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
9093912Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
9103912Slling 		}
9113912Slling 	}
9123912Slling 
9133912Slling 	/*
9143912Slling 	 * Now go through and print the headers.
9153912Slling 	 */
91611022STom.Erickson@Sun.COM 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
9173912Slling 		switch (cbp->cb_columns[i]) {
9183912Slling 		case GET_COL_NAME:
9193912Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
9203912Slling 			break;
9213912Slling 		case GET_COL_PROPERTY:
9223912Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
9233912Slling 			break;
9243912Slling 		case GET_COL_VALUE:
9253912Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
9263912Slling 			break;
92711022STom.Erickson@Sun.COM 		case GET_COL_RECVD:
92811022STom.Erickson@Sun.COM 			title = dgettext(TEXT_DOMAIN, "RECEIVED");
92911022STom.Erickson@Sun.COM 			break;
9303912Slling 		case GET_COL_SOURCE:
9313912Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
9323912Slling 			break;
9333912Slling 		default:
9343912Slling 			title = NULL;
9353912Slling 		}
9363912Slling 
9373912Slling 		if (title != NULL) {
93811022STom.Erickson@Sun.COM 			if (i == (ZFS_GET_NCOLS - 1) ||
93911022STom.Erickson@Sun.COM 			    cbp->cb_columns[i + 1] == GET_COL_NONE)
9403912Slling 				(void) printf("%s", title);
9413912Slling 			else
9423912Slling 				(void) printf("%-*s  ",
9433912Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
9443912Slling 				    title);
9453912Slling 		}
9463912Slling 	}
9473912Slling 	(void) printf("\n");
9483912Slling }
9493912Slling 
9503912Slling /*
9513912Slling  * Display a single line of output, according to the settings in the callback
9523912Slling  * structure.
9533912Slling  */
9543912Slling void
zprop_print_one_property(const char * name,zprop_get_cbdata_t * cbp,const char * propname,const char * value,zprop_source_t sourcetype,const char * source,const char * recvd_value)9555094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
9565094Slling     const char *propname, const char *value, zprop_source_t sourcetype,
95711022STom.Erickson@Sun.COM     const char *source, const char *recvd_value)
9583912Slling {
9593912Slling 	int i;
9603912Slling 	const char *str;
9613912Slling 	char buf[128];
9623912Slling 
9633912Slling 	/*
9643912Slling 	 * Ignore those source types that the user has chosen to ignore.
9653912Slling 	 */
9663912Slling 	if ((sourcetype & cbp->cb_sources) == 0)
9673912Slling 		return;
9683912Slling 
9693912Slling 	if (cbp->cb_first)
9705094Slling 		zprop_print_headers(cbp, cbp->cb_type);
9713912Slling 
97211022STom.Erickson@Sun.COM 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
9733912Slling 		switch (cbp->cb_columns[i]) {
9743912Slling 		case GET_COL_NAME:
9753912Slling 			str = name;
9763912Slling 			break;
9773912Slling 
9783912Slling 		case GET_COL_PROPERTY:
9793912Slling 			str = propname;
9803912Slling 			break;
9813912Slling 
9823912Slling 		case GET_COL_VALUE:
9833912Slling 			str = value;
9843912Slling 			break;
9853912Slling 
9863912Slling 		case GET_COL_SOURCE:
9873912Slling 			switch (sourcetype) {
9885094Slling 			case ZPROP_SRC_NONE:
9893912Slling 				str = "-";
9903912Slling 				break;
9913912Slling 
9925094Slling 			case ZPROP_SRC_DEFAULT:
9933912Slling 				str = "default";
9943912Slling 				break;
9953912Slling 
9965094Slling 			case ZPROP_SRC_LOCAL:
9973912Slling 				str = "local";
9983912Slling 				break;
9993912Slling 
10005094Slling 			case ZPROP_SRC_TEMPORARY:
10013912Slling 				str = "temporary";
10023912Slling 				break;
10033912Slling 
10045094Slling 			case ZPROP_SRC_INHERITED:
10053912Slling 				(void) snprintf(buf, sizeof (buf),
10063912Slling 				    "inherited from %s", source);
10073912Slling 				str = buf;
10083912Slling 				break;
100911022STom.Erickson@Sun.COM 			case ZPROP_SRC_RECEIVED:
101011022STom.Erickson@Sun.COM 				str = "received";
101111022STom.Erickson@Sun.COM 				break;
10123912Slling 			}
10133912Slling 			break;
10143912Slling 
101511022STom.Erickson@Sun.COM 		case GET_COL_RECVD:
101611022STom.Erickson@Sun.COM 			str = (recvd_value == NULL ? "-" : recvd_value);
101711022STom.Erickson@Sun.COM 			break;
101811022STom.Erickson@Sun.COM 
10193912Slling 		default:
10203912Slling 			continue;
10213912Slling 		}
10223912Slling 
102311022STom.Erickson@Sun.COM 		if (cbp->cb_columns[i + 1] == GET_COL_NONE)
10243912Slling 			(void) printf("%s", str);
10253912Slling 		else if (cbp->cb_scripted)
10263912Slling 			(void) printf("%s\t", str);
10273912Slling 		else
10283912Slling 			(void) printf("%-*s  ",
10293912Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
10303912Slling 			    str);
10313912Slling 	}
10323912Slling 
10333912Slling 	(void) printf("\n");
10343912Slling }
10354543Smarks 
10365094Slling /*
10375094Slling  * Given a numeric suffix, convert the value into a number of bits that the
10385094Slling  * resulting value must be shifted.
10395094Slling  */
10405094Slling static int
str2shift(libzfs_handle_t * hdl,const char * buf)10415094Slling str2shift(libzfs_handle_t *hdl, const char *buf)
10425094Slling {
10435094Slling 	const char *ends = "BKMGTPEZ";
10445094Slling 	int i;
10455094Slling 
10465094Slling 	if (buf[0] == '\0')
10475094Slling 		return (0);
10485094Slling 	for (i = 0; i < strlen(ends); i++) {
10495094Slling 		if (toupper(buf[0]) == ends[i])
10505094Slling 			break;
10515094Slling 	}
10525094Slling 	if (i == strlen(ends)) {
10535094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10545094Slling 		    "invalid numeric suffix '%s'"), buf);
10555094Slling 		return (-1);
10565094Slling 	}
10575094Slling 
10585094Slling 	/*
10595094Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
10605094Slling 	 * allow 'BB' - that's just weird.
10615094Slling 	 */
10625094Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
10635094Slling 	    toupper(buf[0]) != 'B'))
10645094Slling 		return (10*i);
10655094Slling 
10665094Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10675094Slling 	    "invalid numeric suffix '%s'"), buf);
10685094Slling 	return (-1);
10695094Slling }
10705094Slling 
10715094Slling /*
10725094Slling  * Convert a string of the form '100G' into a real number.  Used when setting
10735094Slling  * properties or creating a volume.  'buf' is used to place an extended error
10745094Slling  * message for the caller to use.
10755094Slling  */
10764543Smarks int
zfs_nicestrtonum(libzfs_handle_t * hdl,const char * value,uint64_t * num)10775094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
10785094Slling {
10795094Slling 	char *end;
10805094Slling 	int shift;
10815094Slling 
10825094Slling 	*num = 0;
10835094Slling 
10845094Slling 	/* Check to see if this looks like a number.  */
10855094Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
10865094Slling 		if (hdl)
10875094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10885094Slling 			    "bad numeric value '%s'"), value);
10895094Slling 		return (-1);
10905094Slling 	}
10915094Slling 
109210817SEric.Schrock@Sun.COM 	/* Rely on strtoull() to process the numeric portion.  */
10935094Slling 	errno = 0;
10948343SEric.Schrock@Sun.COM 	*num = strtoull(value, &end, 10);
10955094Slling 
10965094Slling 	/*
10975094Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
10985094Slling 	 * in a 64-bit value.
10995094Slling 	 */
11005094Slling 	if (errno == ERANGE) {
11015094Slling 		if (hdl)
11025094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11035094Slling 			    "numeric value is too large"));
11045094Slling 		return (-1);
11055094Slling 	}
11065094Slling 
11075094Slling 	/*
11085094Slling 	 * If we have a decimal value, then do the computation with floating
11095094Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
11105094Slling 	 */
11115094Slling 	if (*end == '.') {
11125094Slling 		double fval = strtod(value, &end);
11135094Slling 
11145094Slling 		if ((shift = str2shift(hdl, end)) == -1)
11155094Slling 			return (-1);
11165094Slling 
11175094Slling 		fval *= pow(2, shift);
11185094Slling 
11195094Slling 		if (fval > UINT64_MAX) {
11205094Slling 			if (hdl)
11215094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11225094Slling 				    "numeric value is too large"));
11235094Slling 			return (-1);
11245094Slling 		}
11255094Slling 
11265094Slling 		*num = (uint64_t)fval;
11275094Slling 	} else {
11285094Slling 		if ((shift = str2shift(hdl, end)) == -1)
11295094Slling 			return (-1);
11305094Slling 
11315094Slling 		/* Check for overflow */
11325094Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
11335094Slling 			if (hdl)
11345094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11355094Slling 				    "numeric value is too large"));
11365094Slling 			return (-1);
11375094Slling 		}
11385094Slling 
11395094Slling 		*num <<= shift;
11405094Slling 	}
11415094Slling 
11425094Slling 	return (0);
11435094Slling }
11445094Slling 
11455094Slling /*
11465094Slling  * Given a propname=value nvpair to set, parse any numeric properties
11475094Slling  * (index, boolean, etc) if they are specified as strings and add the
11485094Slling  * resulting nvpair to the returned nvlist.
11495094Slling  *
11505094Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
11515094Slling  * We want the user to be able to ignore this fact and specify properties
11525094Slling  * as native values (numbers, for example) or as strings (to simplify
11535094Slling  * command line utilities).  This also handles converting index types
11545094Slling  * (compression, checksum, etc) from strings to their on-disk index.
11555094Slling  */
11565094Slling int
zprop_parse_value(libzfs_handle_t * hdl,nvpair_t * elem,int prop,zfs_type_t type,nvlist_t * ret,char ** svalp,uint64_t * ivalp,const char * errbuf)11575094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
11585094Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
11595094Slling     const char *errbuf)
11604543Smarks {
11615094Slling 	data_type_t datatype = nvpair_type(elem);
11625094Slling 	zprop_type_t proptype;
11635094Slling 	const char *propname;
11645094Slling 	char *value;
11655094Slling 	boolean_t isnone = B_FALSE;
11665094Slling 
11675094Slling 	if (type == ZFS_TYPE_POOL) {
11685094Slling 		proptype = zpool_prop_get_type(prop);
11695094Slling 		propname = zpool_prop_to_name(prop);
11705094Slling 	} else {
11715094Slling 		proptype = zfs_prop_get_type(prop);
11725094Slling 		propname = zfs_prop_to_name(prop);
11735094Slling 	}
11745094Slling 
11755094Slling 	/*
11765094Slling 	 * Convert any properties to the internal DSL value types.
11775094Slling 	 */
11785094Slling 	*svalp = NULL;
11795094Slling 	*ivalp = 0;
11805094Slling 
11815094Slling 	switch (proptype) {
11825094Slling 	case PROP_TYPE_STRING:
11835094Slling 		if (datatype != DATA_TYPE_STRING) {
11845094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11855094Slling 			    "'%s' must be a string"), nvpair_name(elem));
11865094Slling 			goto error;
11875094Slling 		}
11885094Slling 		(void) nvpair_value_string(elem, svalp);
11895094Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
11905094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11915094Slling 			    "'%s' is too long"), nvpair_name(elem));
11925094Slling 			goto error;
11935094Slling 		}
11945094Slling 		break;
11955094Slling 
11965094Slling 	case PROP_TYPE_NUMBER:
11975094Slling 		if (datatype == DATA_TYPE_STRING) {
11985094Slling 			(void) nvpair_value_string(elem, &value);
11995094Slling 			if (strcmp(value, "none") == 0) {
12005094Slling 				isnone = B_TRUE;
12015094Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
12025094Slling 			    != 0) {
12035094Slling 				goto error;
12045094Slling 			}
12055094Slling 		} else if (datatype == DATA_TYPE_UINT64) {
12065094Slling 			(void) nvpair_value_uint64(elem, ivalp);
12075094Slling 		} else {
12085094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12095094Slling 			    "'%s' must be a number"), nvpair_name(elem));
12105094Slling 			goto error;
12115094Slling 		}
12125094Slling 
12135094Slling 		/*
12145094Slling 		 * Quota special: force 'none' and don't allow 0.
12155094Slling 		 */
12165378Sck153898 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
12175378Sck153898 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
12185094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12195378Sck153898 			    "use 'none' to disable quota/refquota"));
12205094Slling 			goto error;
12215094Slling 		}
12225094Slling 		break;
12235094Slling 
12245094Slling 	case PROP_TYPE_INDEX:
12255378Sck153898 		if (datatype != DATA_TYPE_STRING) {
12265094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12275094Slling 			    "'%s' must be a string"), nvpair_name(elem));
12285094Slling 			goto error;
12295094Slling 		}
12305094Slling 
12315378Sck153898 		(void) nvpair_value_string(elem, &value);
12325378Sck153898 
12335094Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
12345094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12355094Slling 			    "'%s' must be one of '%s'"), propname,
12365094Slling 			    zprop_values(prop, type));
12375094Slling 			goto error;
12385094Slling 		}
12395094Slling 		break;
12405094Slling 
12415094Slling 	default:
12425094Slling 		abort();
12435094Slling 	}
12444543Smarks 
12455094Slling 	/*
12465094Slling 	 * Add the result to our return set of properties.
12475094Slling 	 */
12485094Slling 	if (*svalp != NULL) {
12495094Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
12505094Slling 			(void) no_memory(hdl);
12515094Slling 			return (-1);
12525094Slling 		}
12535094Slling 	} else {
12545094Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
12555094Slling 			(void) no_memory(hdl);
12565094Slling 			return (-1);
12575094Slling 		}
12585094Slling 	}
12595094Slling 
12605094Slling 	return (0);
12615094Slling error:
12625094Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
12635094Slling 	return (-1);
12645094Slling }
12655094Slling 
12667390SMatthew.Ahrens@Sun.COM static int
addlist(libzfs_handle_t * hdl,char * propname,zprop_list_t ** listp,zfs_type_t type)12677390SMatthew.Ahrens@Sun.COM addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
12687390SMatthew.Ahrens@Sun.COM     zfs_type_t type)
12697390SMatthew.Ahrens@Sun.COM {
12707390SMatthew.Ahrens@Sun.COM 	int prop;
12717390SMatthew.Ahrens@Sun.COM 	zprop_list_t *entry;
12727390SMatthew.Ahrens@Sun.COM 
12737390SMatthew.Ahrens@Sun.COM 	prop = zprop_name_to_prop(propname, type);
12747390SMatthew.Ahrens@Sun.COM 
12757390SMatthew.Ahrens@Sun.COM 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
12767390SMatthew.Ahrens@Sun.COM 		prop = ZPROP_INVAL;
12777390SMatthew.Ahrens@Sun.COM 
12787390SMatthew.Ahrens@Sun.COM 	/*
12797390SMatthew.Ahrens@Sun.COM 	 * When no property table entry can be found, return failure if
12807390SMatthew.Ahrens@Sun.COM 	 * this is a pool property or if this isn't a user-defined
12817390SMatthew.Ahrens@Sun.COM 	 * dataset property,
12827390SMatthew.Ahrens@Sun.COM 	 */
12837390SMatthew.Ahrens@Sun.COM 	if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
12849396SMatthew.Ahrens@Sun.COM 	    (!zfs_prop_user(propname) && !zfs_prop_userquota(propname)))) {
12857390SMatthew.Ahrens@Sun.COM 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12867390SMatthew.Ahrens@Sun.COM 		    "invalid property '%s'"), propname);
12877390SMatthew.Ahrens@Sun.COM 		return (zfs_error(hdl, EZFS_BADPROP,
12887390SMatthew.Ahrens@Sun.COM 		    dgettext(TEXT_DOMAIN, "bad property list")));
12897390SMatthew.Ahrens@Sun.COM 	}
12907390SMatthew.Ahrens@Sun.COM 
12917390SMatthew.Ahrens@Sun.COM 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
12927390SMatthew.Ahrens@Sun.COM 		return (-1);
12937390SMatthew.Ahrens@Sun.COM 
12947390SMatthew.Ahrens@Sun.COM 	entry->pl_prop = prop;
12957390SMatthew.Ahrens@Sun.COM 	if (prop == ZPROP_INVAL) {
12967390SMatthew.Ahrens@Sun.COM 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) == NULL) {
12977390SMatthew.Ahrens@Sun.COM 			free(entry);
12987390SMatthew.Ahrens@Sun.COM 			return (-1);
12997390SMatthew.Ahrens@Sun.COM 		}
13007390SMatthew.Ahrens@Sun.COM 		entry->pl_width = strlen(propname);
13017390SMatthew.Ahrens@Sun.COM 	} else {
13027390SMatthew.Ahrens@Sun.COM 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
13037390SMatthew.Ahrens@Sun.COM 		    type);
13047390SMatthew.Ahrens@Sun.COM 	}
13057390SMatthew.Ahrens@Sun.COM 
13067390SMatthew.Ahrens@Sun.COM 	*listp = entry;
13077390SMatthew.Ahrens@Sun.COM 
13087390SMatthew.Ahrens@Sun.COM 	return (0);
13097390SMatthew.Ahrens@Sun.COM }
13107390SMatthew.Ahrens@Sun.COM 
13115094Slling /*
13125094Slling  * Given a comma-separated list of properties, construct a property list
13135094Slling  * containing both user-defined and native properties.  This function will
13145094Slling  * return a NULL list if 'all' is specified, which can later be expanded
13155094Slling  * by zprop_expand_list().
13165094Slling  */
13175094Slling int
zprop_get_list(libzfs_handle_t * hdl,char * props,zprop_list_t ** listp,zfs_type_t type)13185094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
13195094Slling     zfs_type_t type)
13205094Slling {
13215094Slling 	*listp = NULL;
13225094Slling 
13235094Slling 	/*
13245094Slling 	 * If 'all' is specified, return a NULL list.
13255094Slling 	 */
13265094Slling 	if (strcmp(props, "all") == 0)
13275094Slling 		return (0);
13285094Slling 
13295094Slling 	/*
13305094Slling 	 * If no props were specified, return an error.
13315094Slling 	 */
13325094Slling 	if (props[0] == '\0') {
13335094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13345094Slling 		    "no properties specified"));
13355094Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
13365094Slling 		    "bad property list")));
13374543Smarks 	}
13385094Slling 
13395094Slling 	/*
13405094Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
13415094Slling 	 * aliases makes this more effort than it's worth.
13425094Slling 	 */
13437390SMatthew.Ahrens@Sun.COM 	while (*props != '\0') {
13447390SMatthew.Ahrens@Sun.COM 		size_t len;
13457390SMatthew.Ahrens@Sun.COM 		char *p;
13467390SMatthew.Ahrens@Sun.COM 		char c;
13477390SMatthew.Ahrens@Sun.COM 
13487390SMatthew.Ahrens@Sun.COM 		if ((p = strchr(props, ',')) == NULL) {
13497390SMatthew.Ahrens@Sun.COM 			len = strlen(props);
13507390SMatthew.Ahrens@Sun.COM 			p = props + len;
13515094Slling 		} else {
13527390SMatthew.Ahrens@Sun.COM 			len = p - props;
13535094Slling 		}
13545094Slling 
13555094Slling 		/*
13565094Slling 		 * Check for empty options.
13575094Slling 		 */
13585094Slling 		if (len == 0) {
13595094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13605094Slling 			    "empty property name"));
13615094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
13625094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
13635094Slling 		}
13645094Slling 
13655094Slling 		/*
13665094Slling 		 * Check all regular property names.
13675094Slling 		 */
13687390SMatthew.Ahrens@Sun.COM 		c = props[len];
13697390SMatthew.Ahrens@Sun.COM 		props[len] = '\0';
13705094Slling 
13717390SMatthew.Ahrens@Sun.COM 		if (strcmp(props, "space") == 0) {
13727390SMatthew.Ahrens@Sun.COM 			static char *spaceprops[] = {
13737390SMatthew.Ahrens@Sun.COM 				"name", "avail", "used", "usedbysnapshots",
13747390SMatthew.Ahrens@Sun.COM 				"usedbydataset", "usedbyrefreservation",
13757390SMatthew.Ahrens@Sun.COM 				"usedbychildren", NULL
13767390SMatthew.Ahrens@Sun.COM 			};
13777390SMatthew.Ahrens@Sun.COM 			int i;
13787390SMatthew.Ahrens@Sun.COM 
13797390SMatthew.Ahrens@Sun.COM 			for (i = 0; spaceprops[i]; i++) {
13807390SMatthew.Ahrens@Sun.COM 				if (addlist(hdl, spaceprops[i], listp, type))
13817390SMatthew.Ahrens@Sun.COM 					return (-1);
13827390SMatthew.Ahrens@Sun.COM 				listp = &(*listp)->pl_next;
13837390SMatthew.Ahrens@Sun.COM 			}
13847390SMatthew.Ahrens@Sun.COM 		} else {
13857390SMatthew.Ahrens@Sun.COM 			if (addlist(hdl, props, listp, type))
13867390SMatthew.Ahrens@Sun.COM 				return (-1);
13877390SMatthew.Ahrens@Sun.COM 			listp = &(*listp)->pl_next;
13885094Slling 		}
13895094Slling 
13907390SMatthew.Ahrens@Sun.COM 		props = p;
13915094Slling 		if (c == ',')
13927390SMatthew.Ahrens@Sun.COM 			props++;
13935094Slling 	}
13945094Slling 
13955094Slling 	return (0);
13965094Slling }
13975094Slling 
13985094Slling void
zprop_free_list(zprop_list_t * pl)13995094Slling zprop_free_list(zprop_list_t *pl)
14005094Slling {
14015094Slling 	zprop_list_t *next;
14024543Smarks 
14035094Slling 	while (pl != NULL) {
14045094Slling 		next = pl->pl_next;
14055094Slling 		free(pl->pl_user_prop);
14065094Slling 		free(pl);
14075094Slling 		pl = next;
14085094Slling 	}
14095094Slling }
14105094Slling 
14115094Slling typedef struct expand_data {
14125094Slling 	zprop_list_t	**last;
14135094Slling 	libzfs_handle_t	*hdl;
14145094Slling 	zfs_type_t type;
14155094Slling } expand_data_t;
14165094Slling 
14175094Slling int
zprop_expand_list_cb(int prop,void * cb)14185094Slling zprop_expand_list_cb(int prop, void *cb)
14195094Slling {
14205094Slling 	zprop_list_t *entry;
14215094Slling 	expand_data_t *edp = cb;
14225094Slling 
14235094Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
14245094Slling 		return (ZPROP_INVAL);
14255094Slling 
14265094Slling 	entry->pl_prop = prop;
14275094Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
14285094Slling 	entry->pl_all = B_TRUE;
14295094Slling 
14305094Slling 	*(edp->last) = entry;
14315094Slling 	edp->last = &entry->pl_next;
14325094Slling 
14335094Slling 	return (ZPROP_CONT);
14344543Smarks }
14355094Slling 
14365094Slling int
zprop_expand_list(libzfs_handle_t * hdl,zprop_list_t ** plp,zfs_type_t type)14375094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
14385094Slling {
14395094Slling 	zprop_list_t *entry;
14405094Slling 	zprop_list_t **last;
14415094Slling 	expand_data_t exp;
14425094Slling 
14435094Slling 	if (*plp == NULL) {
14445094Slling 		/*
14455094Slling 		 * If this is the very first time we've been called for an 'all'
14465094Slling 		 * specification, expand the list to include all native
14475094Slling 		 * properties.
14485094Slling 		 */
14495094Slling 		last = plp;
14505094Slling 
14515094Slling 		exp.last = last;
14525094Slling 		exp.hdl = hdl;
14535094Slling 		exp.type = type;
14545094Slling 
14555094Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
14565094Slling 		    B_FALSE, type) == ZPROP_INVAL)
14575094Slling 			return (-1);
14585094Slling 
14595094Slling 		/*
14605094Slling 		 * Add 'name' to the beginning of the list, which is handled
14615094Slling 		 * specially.
14625094Slling 		 */
14635094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
14645094Slling 			return (-1);
14655094Slling 
14665094Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
14675094Slling 		    ZFS_PROP_NAME;
14685094Slling 		entry->pl_width = zprop_width(entry->pl_prop,
14695094Slling 		    &entry->pl_fixed, type);
14705094Slling 		entry->pl_all = B_TRUE;
14715094Slling 		entry->pl_next = *plp;
14725094Slling 		*plp = entry;
14735094Slling 	}
14745094Slling 	return (0);
14755094Slling }
14765094Slling 
14775094Slling int
zprop_iter(zprop_func func,void * cb,boolean_t show_all,boolean_t ordered,zfs_type_t type)14785094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
14795094Slling     zfs_type_t type)
14805094Slling {
14815094Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
14825094Slling }
1483