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 /*
223635Sck153898  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
29789Sahrens  * Internal utility routines for the ZFS library.
30789Sahrens  */
31789Sahrens 
32789Sahrens #include <errno.h>
33789Sahrens #include <fcntl.h>
34789Sahrens #include <libintl.h>
35789Sahrens #include <stdarg.h>
36789Sahrens #include <stdio.h>
37789Sahrens #include <stdlib.h>
38789Sahrens #include <strings.h>
39789Sahrens #include <unistd.h>
40*5094Slling #include <ctype.h>
41*5094Slling #include <math.h>
42789Sahrens #include <sys/mnttab.h>
433635Sck153898 #include <sys/mntent.h>
443635Sck153898 #include <sys/types.h>
45789Sahrens 
46789Sahrens #include <libzfs.h>
47789Sahrens 
48789Sahrens #include "libzfs_impl.h"
494787Sahrens #include "zfs_prop.h"
50789Sahrens 
512082Seschrock int
522082Seschrock libzfs_errno(libzfs_handle_t *hdl)
532082Seschrock {
542082Seschrock 	return (hdl->libzfs_error);
552082Seschrock }
56789Sahrens 
572082Seschrock const char *
582082Seschrock libzfs_error_action(libzfs_handle_t *hdl)
592082Seschrock {
602082Seschrock 	return (hdl->libzfs_action);
612082Seschrock }
622082Seschrock 
632082Seschrock const char *
642082Seschrock libzfs_error_description(libzfs_handle_t *hdl)
652082Seschrock {
662082Seschrock 	if (hdl->libzfs_desc[0] != '\0')
672082Seschrock 		return (hdl->libzfs_desc);
68789Sahrens 
692082Seschrock 	switch (hdl->libzfs_error) {
702082Seschrock 	case EZFS_NOMEM:
712082Seschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
722082Seschrock 	case EZFS_BADPROP:
732082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
742082Seschrock 	case EZFS_PROPREADONLY:
752082Seschrock 		return (dgettext(TEXT_DOMAIN, "read only property"));
762082Seschrock 	case EZFS_PROPTYPE:
772082Seschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
782082Seschrock 		    "datasets of this type"));
792082Seschrock 	case EZFS_PROPNONINHERIT:
802082Seschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
812082Seschrock 	case EZFS_PROPSPACE:
822082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
832082Seschrock 	case EZFS_BADTYPE:
842082Seschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
852082Seschrock 		    "datasets of this type"));
862082Seschrock 	case EZFS_BUSY:
872082Seschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
882082Seschrock 	case EZFS_EXISTS:
892082Seschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
902082Seschrock 	case EZFS_NOENT:
912082Seschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
922082Seschrock 	case EZFS_BADSTREAM:
932082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
942082Seschrock 	case EZFS_DSREADONLY:
952082Seschrock 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
962082Seschrock 	case EZFS_VOLTOOBIG:
972082Seschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
982082Seschrock 		    "this system"));
992082Seschrock 	case EZFS_VOLHASDATA:
1002082Seschrock 		return (dgettext(TEXT_DOMAIN, "volume has data"));
1012082Seschrock 	case EZFS_INVALIDNAME:
1022082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
1032082Seschrock 	case EZFS_BADRESTORE:
1042082Seschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
1052082Seschrock 		    "destination"));
1062082Seschrock 	case EZFS_BADBACKUP:
1072082Seschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
1082082Seschrock 	case EZFS_BADTARGET:
1092082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
1102082Seschrock 	case EZFS_NODEVICE:
1112082Seschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
1122082Seschrock 	case EZFS_BADDEV:
1132082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
1142082Seschrock 	case EZFS_NOREPLICAS:
1152082Seschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
1162082Seschrock 	case EZFS_RESILVERING:
1172082Seschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
1182082Seschrock 	case EZFS_BADVERSION:
1192082Seschrock 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
1202082Seschrock 	case EZFS_POOLUNAVAIL:
1212082Seschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
1222082Seschrock 	case EZFS_DEVOVERFLOW:
1232082Seschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
1242082Seschrock 	case EZFS_BADPATH:
1252082Seschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
1262082Seschrock 	case EZFS_CROSSTARGET:
1272082Seschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
1282082Seschrock 		    "pools"));
1292082Seschrock 	case EZFS_ZONED:
1302082Seschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
1312082Seschrock 	case EZFS_MOUNTFAILED:
1322082Seschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
1332082Seschrock 	case EZFS_UMOUNTFAILED:
1342082Seschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
1353126Sahl 	case EZFS_UNSHARENFSFAILED:
1362082Seschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
1373126Sahl 	case EZFS_SHARENFSFAILED:
1382082Seschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
1394543Smarks 	case EZFS_ISCSISVCUNAVAIL:
1404543Smarks 		return (dgettext(TEXT_DOMAIN,
1414543Smarks 		    "iscsitgt service need to be enabled by "
1424543Smarks 		    "a privileged user"));
1432082Seschrock 	case EZFS_DEVLINKS:
1442082Seschrock 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
1452082Seschrock 	case EZFS_PERM:
1462082Seschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
1472082Seschrock 	case EZFS_NOSPC:
1482082Seschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
1492082Seschrock 	case EZFS_IO:
1502082Seschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
1512082Seschrock 	case EZFS_INTR:
1522082Seschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
1532082Seschrock 	case EZFS_ISSPARE:
1542082Seschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
1552082Seschrock 		    "spare"));
1562082Seschrock 	case EZFS_INVALCONFIG:
1572082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1582474Seschrock 	case EZFS_RECURSIVE:
1592474Seschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
1602926Sek110237 	case EZFS_NOHISTORY:
1612926Sek110237 		return (dgettext(TEXT_DOMAIN, "no history available"));
1623126Sahl 	case EZFS_UNSHAREISCSIFAILED:
1633126Sahl 		return (dgettext(TEXT_DOMAIN,
1643126Sahl 		    "iscsitgtd failed request to unshare"));
1653126Sahl 	case EZFS_SHAREISCSIFAILED:
1663126Sahl 		return (dgettext(TEXT_DOMAIN,
1673126Sahl 		    "iscsitgtd failed request to share"));
1683912Slling 	case EZFS_POOLPROPS:
1693912Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
1703912Slling 		    "pool properties"));
1713912Slling 	case EZFS_POOL_NOTSUP:
1723912Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
1733912Slling 		    "on this type of pool"));
1743912Slling 	case EZFS_POOL_INVALARG:
1753912Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
1763912Slling 		    "this pool operation"));
1773978Smmusante 	case EZFS_NAMETOOLONG:
1783978Smmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1794276Staylor 	case EZFS_OPENFAILED:
1804276Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1814276Staylor 	case EZFS_NOCAP:
1824276Staylor 		return (dgettext(TEXT_DOMAIN,
1834276Staylor 		    "disk capacity information could not be retrieved"));
1844276Staylor 	case EZFS_LABELFAILED:
1854276Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
1864543Smarks 	case EZFS_BADWHO:
1874543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
1884543Smarks 	case EZFS_BADPERM:
1894543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
1904543Smarks 	case EZFS_BADPERMSET:
1914543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
1924543Smarks 	case EZFS_NODELEGATION:
1934543Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
1944543Smarks 		    "disabled on pool"));
1954543Smarks 	case EZFS_PERMRDONLY:
1964543Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
1974543Smarks 		    " modified"));
1982082Seschrock 	case EZFS_UNKNOWN:
1992082Seschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
2002082Seschrock 	default:
2012500Seschrock 		assert(hdl->libzfs_error == 0);
2022500Seschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
2032082Seschrock 	}
2042082Seschrock }
2052082Seschrock 
2062082Seschrock /*PRINTFLIKE2*/
207789Sahrens void
2082082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
209789Sahrens {
210789Sahrens 	va_list ap;
211789Sahrens 
212789Sahrens 	va_start(ap, fmt);
213789Sahrens 
2142082Seschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
2152082Seschrock 	    fmt, ap);
2162082Seschrock 	hdl->libzfs_desc_active = 1;
217789Sahrens 
218789Sahrens 	va_end(ap);
219789Sahrens }
220789Sahrens 
2212082Seschrock static void
2222082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
2232082Seschrock {
2242082Seschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
2252082Seschrock 	    fmt, ap);
2262082Seschrock 	hdl->libzfs_error = error;
2272082Seschrock 
2282082Seschrock 	if (hdl->libzfs_desc_active)
2292082Seschrock 		hdl->libzfs_desc_active = 0;
2302082Seschrock 	else
2312082Seschrock 		hdl->libzfs_desc[0] = '\0';
2322082Seschrock 
2332082Seschrock 	if (hdl->libzfs_printerr) {
2342082Seschrock 		if (error == EZFS_UNKNOWN) {
2352082Seschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
2362082Seschrock 			    "error: %s\n"), libzfs_error_description(hdl));
2372082Seschrock 			abort();
2382082Seschrock 		}
2392082Seschrock 
2402082Seschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
2413912Slling 		    libzfs_error_description(hdl));
2422082Seschrock 		if (error == EZFS_NOMEM)
2432082Seschrock 			exit(1);
2442082Seschrock 	}
2452082Seschrock }
2462082Seschrock 
2473237Slling int
2483237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
2493237Slling {
2503237Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
2513237Slling }
2523237Slling 
2532082Seschrock /*PRINTFLIKE3*/
2542082Seschrock int
2553237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
2562082Seschrock {
2572082Seschrock 	va_list ap;
2582082Seschrock 
2592082Seschrock 	va_start(ap, fmt);
2602082Seschrock 
2612082Seschrock 	zfs_verror(hdl, error, fmt, ap);
2622082Seschrock 
2632082Seschrock 	va_end(ap);
2642082Seschrock 
2652082Seschrock 	return (-1);
2662082Seschrock }
2672082Seschrock 
2682082Seschrock static int
2692082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
2702082Seschrock     va_list ap)
2712082Seschrock {
2722082Seschrock 	switch (error) {
2732082Seschrock 	case EPERM:
2742082Seschrock 	case EACCES:
2752082Seschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
2762082Seschrock 		return (-1);
2772082Seschrock 
2784543Smarks 	case ECANCELED:
2794543Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
2804543Smarks 		return (-1);
2814543Smarks 
2822082Seschrock 	case EIO:
2832082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
2842082Seschrock 		return (-1);
2852082Seschrock 
2862082Seschrock 	case EINTR:
2872082Seschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
2882082Seschrock 		return (-1);
2892082Seschrock 	}
2902082Seschrock 
2912082Seschrock 	return (0);
2922082Seschrock }
2932082Seschrock 
2943237Slling int
2953237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
2963237Slling {
2973237Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
2983237Slling }
2993237Slling 
3002082Seschrock /*PRINTFLIKE3*/
3012082Seschrock int
3023237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
303789Sahrens {
304789Sahrens 	va_list ap;
305789Sahrens 
306789Sahrens 	va_start(ap, fmt);
307789Sahrens 
3082082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3092082Seschrock 		va_end(ap);
3102082Seschrock 		return (-1);
3112082Seschrock 	}
3122082Seschrock 
3132082Seschrock 
3142082Seschrock 	switch (error) {
3152082Seschrock 	case ENXIO:
3162082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3172082Seschrock 		break;
3182082Seschrock 
3192082Seschrock 	case ENOENT:
3202082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3212082Seschrock 		    "dataset does not exist"));
3222082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3232082Seschrock 		break;
3242082Seschrock 
3252082Seschrock 	case ENOSPC:
3262082Seschrock 	case EDQUOT:
3272082Seschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
3282082Seschrock 		return (-1);
3292082Seschrock 
3302082Seschrock 	case EEXIST:
3312082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3322082Seschrock 		    "dataset already exists"));
3332082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3342082Seschrock 		break;
3352082Seschrock 
3362082Seschrock 	case EBUSY:
3372082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3382082Seschrock 		    "dataset is busy"));
3392082Seschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
3402082Seschrock 		break;
3414543Smarks 	case EROFS:
3424543Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3434543Smarks 		    "snapshot permissions cannot be modified"));
3444543Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
3454543Smarks 		break;
3463978Smmusante 	case ENAMETOOLONG:
3473978Smmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
3483978Smmusante 		break;
3492082Seschrock 	default:
3502082Seschrock 		zfs_error_aux(hdl, strerror(errno));
3512082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
3522082Seschrock 		break;
353789Sahrens 	}
354789Sahrens 
355789Sahrens 	va_end(ap);
3562082Seschrock 	return (-1);
357789Sahrens }
358789Sahrens 
3593237Slling int
3603237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3613237Slling {
3623237Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
3633237Slling }
3643237Slling 
3652082Seschrock /*PRINTFLIKE3*/
3662082Seschrock int
3673237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
368789Sahrens {
3692082Seschrock 	va_list ap;
3702082Seschrock 
3712082Seschrock 	va_start(ap, fmt);
3722082Seschrock 
3732082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3742082Seschrock 		va_end(ap);
3752082Seschrock 		return (-1);
3762082Seschrock 	}
3772082Seschrock 
3782082Seschrock 	switch (error) {
3792082Seschrock 	case ENODEV:
3802082Seschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
3812082Seschrock 		break;
3822082Seschrock 
3832082Seschrock 	case ENOENT:
3843912Slling 		zfs_error_aux(hdl,
3853912Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
3862082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3872082Seschrock 		break;
3882082Seschrock 
3892082Seschrock 	case EEXIST:
3902082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3912082Seschrock 		    "pool already exists"));
3922082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3932082Seschrock 		break;
3942082Seschrock 
3952082Seschrock 	case EBUSY:
3962082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
3972082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3982082Seschrock 		break;
3992082Seschrock 
4002082Seschrock 	case ENXIO:
4012082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4022082Seschrock 		    "one or more devices is currently unavailable"));
4032082Seschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
4042082Seschrock 		break;
4052082Seschrock 
4062082Seschrock 	case ENAMETOOLONG:
4072082Seschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
4082082Seschrock 		break;
4092082Seschrock 
4103912Slling 	case ENOTSUP:
4113912Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
4123912Slling 		break;
4133912Slling 
4143912Slling 	case EINVAL:
4153912Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
4163912Slling 		break;
4173912Slling 
4184543Smarks 	case ENOSPC:
4194543Smarks 	case EDQUOT:
4204543Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
4214543Smarks 		return (-1);
4224543Smarks 
4232082Seschrock 	default:
4242082Seschrock 		zfs_error_aux(hdl, strerror(error));
4252082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
4262082Seschrock 	}
4272082Seschrock 
4282082Seschrock 	va_end(ap);
4292082Seschrock 	return (-1);
430789Sahrens }
431789Sahrens 
432789Sahrens /*
433789Sahrens  * Display an out of memory error message and abort the current program.
434789Sahrens  */
4352082Seschrock int
4362082Seschrock no_memory(libzfs_handle_t *hdl)
437789Sahrens {
4382082Seschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
439789Sahrens }
440789Sahrens 
441789Sahrens /*
442789Sahrens  * A safe form of malloc() which will die if the allocation fails.
443789Sahrens  */
444789Sahrens void *
4452082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
446789Sahrens {
447789Sahrens 	void *data;
448789Sahrens 
449789Sahrens 	if ((data = calloc(1, size)) == NULL)
4502082Seschrock 		(void) no_memory(hdl);
451789Sahrens 
452789Sahrens 	return (data);
453789Sahrens }
454789Sahrens 
455789Sahrens /*
4562676Seschrock  * A safe form of realloc(), which also zeroes newly allocated space.
4572676Seschrock  */
4582676Seschrock void *
4592676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
4602676Seschrock {
4612676Seschrock 	void *ret;
4622676Seschrock 
4632676Seschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
4642676Seschrock 		(void) no_memory(hdl);
4652676Seschrock 		free(ptr);
4662676Seschrock 		return (NULL);
4672676Seschrock 	}
4682676Seschrock 
4692676Seschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
4702676Seschrock 	return (ret);
4712676Seschrock }
4722676Seschrock 
4732676Seschrock /*
474789Sahrens  * A safe form of strdup() which will die if the allocation fails.
475789Sahrens  */
476789Sahrens char *
4772082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
478789Sahrens {
479789Sahrens 	char *ret;
480789Sahrens 
481789Sahrens 	if ((ret = strdup(str)) == NULL)
4822082Seschrock 		(void) no_memory(hdl);
483789Sahrens 
484789Sahrens 	return (ret);
485789Sahrens }
486789Sahrens 
487789Sahrens /*
488789Sahrens  * Convert a number to an appropriately human-readable output.
489789Sahrens  */
490789Sahrens void
491789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
492789Sahrens {
493789Sahrens 	uint64_t n = num;
494789Sahrens 	int index = 0;
495789Sahrens 	char u;
496789Sahrens 
497789Sahrens 	while (n >= 1024) {
4981162Seschrock 		n /= 1024;
499789Sahrens 		index++;
500789Sahrens 	}
501789Sahrens 
502789Sahrens 	u = " KMGTPE"[index];
503789Sahrens 
5041162Seschrock 	if (index == 0) {
505789Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5061162Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5071162Seschrock 		/*
5081162Seschrock 		 * If this is an even multiple of the base, always display
5091162Seschrock 		 * without any decimal precision.
5101162Seschrock 		 */
511789Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5121162Seschrock 	} else {
5131162Seschrock 		/*
5141162Seschrock 		 * We want to choose a precision that reflects the best choice
5151162Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5161162Seschrock 		 * we have numbers that are very close to an order of magnitude.
5171162Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5181162Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5191162Seschrock 		 * develop some complex heuristics for this, but it's much
5201162Seschrock 		 * easier just to try each combination in turn.
5211162Seschrock 		 */
5221162Seschrock 		int i;
5231162Seschrock 		for (i = 2; i >= 0; i--) {
5244451Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5254451Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5261162Seschrock 				break;
5271162Seschrock 		}
5281162Seschrock 	}
529789Sahrens }
5302082Seschrock 
5312082Seschrock void
5322082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
5332082Seschrock {
5342082Seschrock 	hdl->libzfs_printerr = printerr;
5352082Seschrock }
5362082Seschrock 
5372082Seschrock libzfs_handle_t *
5382082Seschrock libzfs_init(void)
5392082Seschrock {
5402082Seschrock 	libzfs_handle_t *hdl;
5412082Seschrock 
5422082Seschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
5432082Seschrock 		return (NULL);
5442082Seschrock 	}
5452082Seschrock 
5462500Seschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
5472082Seschrock 		free(hdl);
5482082Seschrock 		return (NULL);
5492082Seschrock 	}
5502082Seschrock 
5512082Seschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
5522082Seschrock 		(void) close(hdl->libzfs_fd);
5532082Seschrock 		free(hdl);
5542082Seschrock 		return (NULL);
5552082Seschrock 	}
5562082Seschrock 
5572082Seschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
5582082Seschrock 
5594787Sahrens 	zfs_prop_init();
560*5094Slling 	zpool_prop_init();
5614787Sahrens 
5622082Seschrock 	return (hdl);
5632082Seschrock }
5642082Seschrock 
5652082Seschrock void
5662082Seschrock libzfs_fini(libzfs_handle_t *hdl)
5672082Seschrock {
5682082Seschrock 	(void) close(hdl->libzfs_fd);
5692082Seschrock 	if (hdl->libzfs_mnttab)
5702082Seschrock 		(void) fclose(hdl->libzfs_mnttab);
5712082Seschrock 	if (hdl->libzfs_sharetab)
5722082Seschrock 		(void) fclose(hdl->libzfs_sharetab);
5734180Sdougm 	zfs_uninit_libshare(hdl);
5744543Smarks 	if (hdl->libzfs_log_str)
5754543Smarks 		(void) free(hdl->libzfs_log_str);
5762082Seschrock 	namespace_clear(hdl);
5772082Seschrock 	free(hdl);
5782082Seschrock }
5792082Seschrock 
5802082Seschrock libzfs_handle_t *
5812082Seschrock zpool_get_handle(zpool_handle_t *zhp)
5822082Seschrock {
5832082Seschrock 	return (zhp->zpool_hdl);
5842082Seschrock }
5852082Seschrock 
5862082Seschrock libzfs_handle_t *
5872082Seschrock zfs_get_handle(zfs_handle_t *zhp)
5882082Seschrock {
5892082Seschrock 	return (zhp->zfs_hdl);
5902082Seschrock }
5912676Seschrock 
5922676Seschrock /*
5933635Sck153898  * Given a name, determine whether or not it's a valid path
5943635Sck153898  * (starts with '/' or "./").  If so, walk the mnttab trying
5953635Sck153898  * to match the device number.  If not, treat the path as an
5963635Sck153898  * fs/vol/snap name.
5973635Sck153898  */
5983635Sck153898 zfs_handle_t *
5993635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6003635Sck153898 {
6013635Sck153898 	struct stat64 statbuf;
6023635Sck153898 	struct extmnttab entry;
6033635Sck153898 	int ret;
6043635Sck153898 
6053635Sck153898 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6063635Sck153898 		/*
6073635Sck153898 		 * It's not a valid path, assume it's a name of type 'argtype'.
6083635Sck153898 		 */
6093635Sck153898 		return (zfs_open(hdl, path, argtype));
6103635Sck153898 	}
6113635Sck153898 
6123635Sck153898 	if (stat64(path, &statbuf) != 0) {
6133635Sck153898 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6143635Sck153898 		return (NULL);
6153635Sck153898 	}
6163635Sck153898 
6173635Sck153898 	rewind(hdl->libzfs_mnttab);
6183635Sck153898 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6193635Sck153898 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6203635Sck153898 		    statbuf.st_dev) {
6213635Sck153898 			break;
6223635Sck153898 		}
6233635Sck153898 	}
6243635Sck153898 	if (ret != 0) {
6253635Sck153898 		return (NULL);
6263635Sck153898 	}
6273635Sck153898 
6283635Sck153898 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6293635Sck153898 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6303635Sck153898 		    path);
6313635Sck153898 		return (NULL);
6323635Sck153898 	}
6333635Sck153898 
6343635Sck153898 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6353635Sck153898 }
6363635Sck153898 
6373635Sck153898 /*
6382676Seschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
6392676Seschrock  * an ioctl().
6402676Seschrock  */
6412676Seschrock int
6422676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
6432676Seschrock {
6442676Seschrock 	if (len == 0)
6452885Sahrens 		len = 2048;
6462676Seschrock 	zc->zc_nvlist_dst_size = len;
6472676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
6482676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
6492676Seschrock 		return (-1);
6502676Seschrock 
6512676Seschrock 	return (0);
6522676Seschrock }
6532676Seschrock 
6542676Seschrock /*
6552676Seschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
6562676Seschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
6572676Seschrock  * filled in by the kernel to indicate the actual required size.
6582676Seschrock  */
6592676Seschrock int
6602676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
6612676Seschrock {
6622676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
6632676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
6642676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
6652676Seschrock 	    == NULL)
6662676Seschrock 		return (-1);
6672676Seschrock 
6682676Seschrock 	return (0);
6692676Seschrock }
6702676Seschrock 
6712676Seschrock /*
6722885Sahrens  * Called to free the src and dst nvlists stored in the command structure.
6732676Seschrock  */
6742676Seschrock void
6752676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc)
6762676Seschrock {
677*5094Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
6782676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
6792676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
6802676Seschrock }
6812676Seschrock 
682*5094Slling static int
683*5094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
684*5094Slling     nvlist_t *nvl)
6852676Seschrock {
6862676Seschrock 	char *packed;
6872676Seschrock 	size_t len;
6882676Seschrock 
6892676Seschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
6902676Seschrock 
6912676Seschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
6922676Seschrock 		return (-1);
6932676Seschrock 
6942676Seschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
6952676Seschrock 
696*5094Slling 	*outnv = (uint64_t)(uintptr_t)packed;
697*5094Slling 	*outlen = len;
698*5094Slling 
699*5094Slling 	return (0);
700*5094Slling }
7012676Seschrock 
702*5094Slling int
703*5094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
704*5094Slling {
705*5094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
706*5094Slling 	    &zc->zc_nvlist_conf_size, nvl));
707*5094Slling }
708*5094Slling 
709*5094Slling int
710*5094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
711*5094Slling {
712*5094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
713*5094Slling 	    &zc->zc_nvlist_src_size, nvl));
7142676Seschrock }
7152676Seschrock 
7162676Seschrock /*
7172676Seschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
7182676Seschrock  */
7192676Seschrock int
7202676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
7212676Seschrock {
7222676Seschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
7232676Seschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
7242676Seschrock 		return (no_memory(hdl));
7252676Seschrock 
7262676Seschrock 	return (0);
7272676Seschrock }
7283912Slling 
729*5094Slling int
730*5094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
731*5094Slling {
732*5094Slling 	int error;
733*5094Slling 
734*5094Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
735*5094Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
736*5094Slling 	if (hdl->libzfs_log_str) {
737*5094Slling 		free(hdl->libzfs_log_str);
738*5094Slling 		hdl->libzfs_log_str = NULL;
739*5094Slling 	}
740*5094Slling 	zc->zc_history = 0;
741*5094Slling 
742*5094Slling 	return (error);
743*5094Slling }
744*5094Slling 
745*5094Slling /*
746*5094Slling  * ================================================================
747*5094Slling  * API shared by zfs and zpool property management
748*5094Slling  * ================================================================
749*5094Slling  */
750*5094Slling 
7513912Slling static void
752*5094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
7533912Slling {
754*5094Slling 	zprop_list_t *pl = cbp->cb_proplist;
7553912Slling 	int i;
7563912Slling 	char *title;
7573912Slling 	size_t len;
7583912Slling 
7593912Slling 	cbp->cb_first = B_FALSE;
7603912Slling 	if (cbp->cb_scripted)
7613912Slling 		return;
7623912Slling 
7633912Slling 	/*
7643912Slling 	 * Start with the length of the column headers.
7653912Slling 	 */
7663912Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
7673912Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
7683912Slling 	    "PROPERTY"));
7693912Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
7703912Slling 	    "VALUE"));
7713912Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
7723912Slling 	    "SOURCE"));
7733912Slling 
7743912Slling 	/*
7753912Slling 	 * Go through and calculate the widths for each column.  For the
7763912Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
7773912Slling 	 * inheriting from the longest name.  This is acceptable because in the
7783912Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
7793912Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
7803912Slling 	 * if the name of the property is much longer the any values we find.
7813912Slling 	 */
7823912Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7833912Slling 		/*
7843912Slling 		 * 'PROPERTY' column
7853912Slling 		 */
786*5094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
787*5094Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
788*5094Slling 			    zpool_prop_to_name(pl->pl_prop) :
789*5094Slling 			    zfs_prop_to_name(pl->pl_prop);
790*5094Slling 
791*5094Slling 			len = strlen(propname);
7923912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
7933912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
7943912Slling 		} else {
7953912Slling 			len = strlen(pl->pl_user_prop);
7963912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
7973912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
7983912Slling 		}
7993912Slling 
8003912Slling 		/*
8013912Slling 		 * 'VALUE' column
8023912Slling 		 */
8033912Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
8043912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
8053912Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
8063912Slling 
8073912Slling 		/*
8083912Slling 		 * 'NAME' and 'SOURCE' columns
8093912Slling 		 */
810*5094Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
811*5094Slling 		    ZFS_PROP_NAME) &&
8123912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
8133912Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
8143912Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
8153912Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
8163912Slling 		}
8173912Slling 	}
8183912Slling 
8193912Slling 	/*
8203912Slling 	 * Now go through and print the headers.
8213912Slling 	 */
8223912Slling 	for (i = 0; i < 4; i++) {
8233912Slling 		switch (cbp->cb_columns[i]) {
8243912Slling 		case GET_COL_NAME:
8253912Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
8263912Slling 			break;
8273912Slling 		case GET_COL_PROPERTY:
8283912Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
8293912Slling 			break;
8303912Slling 		case GET_COL_VALUE:
8313912Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
8323912Slling 			break;
8333912Slling 		case GET_COL_SOURCE:
8343912Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
8353912Slling 			break;
8363912Slling 		default:
8373912Slling 			title = NULL;
8383912Slling 		}
8393912Slling 
8403912Slling 		if (title != NULL) {
8413912Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
8423912Slling 				(void) printf("%s", title);
8433912Slling 			else
8443912Slling 				(void) printf("%-*s  ",
8453912Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
8463912Slling 				    title);
8473912Slling 		}
8483912Slling 	}
8493912Slling 	(void) printf("\n");
8503912Slling }
8513912Slling 
8523912Slling /*
8533912Slling  * Display a single line of output, according to the settings in the callback
8543912Slling  * structure.
8553912Slling  */
8563912Slling void
857*5094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
858*5094Slling     const char *propname, const char *value, zprop_source_t sourcetype,
8593912Slling     const char *source)
8603912Slling {
8613912Slling 	int i;
8623912Slling 	const char *str;
8633912Slling 	char buf[128];
8643912Slling 
8653912Slling 	/*
8663912Slling 	 * Ignore those source types that the user has chosen to ignore.
8673912Slling 	 */
8683912Slling 	if ((sourcetype & cbp->cb_sources) == 0)
8693912Slling 		return;
8703912Slling 
8713912Slling 	if (cbp->cb_first)
872*5094Slling 		zprop_print_headers(cbp, cbp->cb_type);
8733912Slling 
8743912Slling 	for (i = 0; i < 4; i++) {
8753912Slling 		switch (cbp->cb_columns[i]) {
8763912Slling 		case GET_COL_NAME:
8773912Slling 			str = name;
8783912Slling 			break;
8793912Slling 
8803912Slling 		case GET_COL_PROPERTY:
8813912Slling 			str = propname;
8823912Slling 			break;
8833912Slling 
8843912Slling 		case GET_COL_VALUE:
8853912Slling 			str = value;
8863912Slling 			break;
8873912Slling 
8883912Slling 		case GET_COL_SOURCE:
8893912Slling 			switch (sourcetype) {
890*5094Slling 			case ZPROP_SRC_NONE:
8913912Slling 				str = "-";
8923912Slling 				break;
8933912Slling 
894*5094Slling 			case ZPROP_SRC_DEFAULT:
8953912Slling 				str = "default";
8963912Slling 				break;
8973912Slling 
898*5094Slling 			case ZPROP_SRC_LOCAL:
8993912Slling 				str = "local";
9003912Slling 				break;
9013912Slling 
902*5094Slling 			case ZPROP_SRC_TEMPORARY:
9033912Slling 				str = "temporary";
9043912Slling 				break;
9053912Slling 
906*5094Slling 			case ZPROP_SRC_INHERITED:
9073912Slling 				(void) snprintf(buf, sizeof (buf),
9083912Slling 				    "inherited from %s", source);
9093912Slling 				str = buf;
9103912Slling 				break;
9113912Slling 			}
9123912Slling 			break;
9133912Slling 
9143912Slling 		default:
9153912Slling 			continue;
9163912Slling 		}
9173912Slling 
9183912Slling 		if (cbp->cb_columns[i + 1] == 0)
9193912Slling 			(void) printf("%s", str);
9203912Slling 		else if (cbp->cb_scripted)
9213912Slling 			(void) printf("%s\t", str);
9223912Slling 		else
9233912Slling 			(void) printf("%-*s  ",
9243912Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
9253912Slling 			    str);
9263912Slling 
9273912Slling 	}
9283912Slling 
9293912Slling 	(void) printf("\n");
9303912Slling }
9314543Smarks 
932*5094Slling /*
933*5094Slling  * Given a numeric suffix, convert the value into a number of bits that the
934*5094Slling  * resulting value must be shifted.
935*5094Slling  */
936*5094Slling static int
937*5094Slling str2shift(libzfs_handle_t *hdl, const char *buf)
938*5094Slling {
939*5094Slling 	const char *ends = "BKMGTPEZ";
940*5094Slling 	int i;
941*5094Slling 
942*5094Slling 	if (buf[0] == '\0')
943*5094Slling 		return (0);
944*5094Slling 	for (i = 0; i < strlen(ends); i++) {
945*5094Slling 		if (toupper(buf[0]) == ends[i])
946*5094Slling 			break;
947*5094Slling 	}
948*5094Slling 	if (i == strlen(ends)) {
949*5094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950*5094Slling 		    "invalid numeric suffix '%s'"), buf);
951*5094Slling 		return (-1);
952*5094Slling 	}
953*5094Slling 
954*5094Slling 	/*
955*5094Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
956*5094Slling 	 * allow 'BB' - that's just weird.
957*5094Slling 	 */
958*5094Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
959*5094Slling 	    toupper(buf[0]) != 'B'))
960*5094Slling 		return (10*i);
961*5094Slling 
962*5094Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
963*5094Slling 	    "invalid numeric suffix '%s'"), buf);
964*5094Slling 	return (-1);
965*5094Slling }
966*5094Slling 
967*5094Slling /*
968*5094Slling  * Convert a string of the form '100G' into a real number.  Used when setting
969*5094Slling  * properties or creating a volume.  'buf' is used to place an extended error
970*5094Slling  * message for the caller to use.
971*5094Slling  */
9724543Smarks int
973*5094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
974*5094Slling {
975*5094Slling 	char *end;
976*5094Slling 	int shift;
977*5094Slling 
978*5094Slling 	*num = 0;
979*5094Slling 
980*5094Slling 	/* Check to see if this looks like a number.  */
981*5094Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
982*5094Slling 		if (hdl)
983*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984*5094Slling 			    "bad numeric value '%s'"), value);
985*5094Slling 		return (-1);
986*5094Slling 	}
987*5094Slling 
988*5094Slling 	/* Rely on stroll() to process the numeric portion.  */
989*5094Slling 	errno = 0;
990*5094Slling 	*num = strtoll(value, &end, 10);
991*5094Slling 
992*5094Slling 	/*
993*5094Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
994*5094Slling 	 * in a 64-bit value.
995*5094Slling 	 */
996*5094Slling 	if (errno == ERANGE) {
997*5094Slling 		if (hdl)
998*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
999*5094Slling 			    "numeric value is too large"));
1000*5094Slling 		return (-1);
1001*5094Slling 	}
1002*5094Slling 
1003*5094Slling 	/*
1004*5094Slling 	 * If we have a decimal value, then do the computation with floating
1005*5094Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1006*5094Slling 	 */
1007*5094Slling 	if (*end == '.') {
1008*5094Slling 		double fval = strtod(value, &end);
1009*5094Slling 
1010*5094Slling 		if ((shift = str2shift(hdl, end)) == -1)
1011*5094Slling 			return (-1);
1012*5094Slling 
1013*5094Slling 		fval *= pow(2, shift);
1014*5094Slling 
1015*5094Slling 		if (fval > UINT64_MAX) {
1016*5094Slling 			if (hdl)
1017*5094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018*5094Slling 				    "numeric value is too large"));
1019*5094Slling 			return (-1);
1020*5094Slling 		}
1021*5094Slling 
1022*5094Slling 		*num = (uint64_t)fval;
1023*5094Slling 	} else {
1024*5094Slling 		if ((shift = str2shift(hdl, end)) == -1)
1025*5094Slling 			return (-1);
1026*5094Slling 
1027*5094Slling 		/* Check for overflow */
1028*5094Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1029*5094Slling 			if (hdl)
1030*5094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031*5094Slling 				    "numeric value is too large"));
1032*5094Slling 			return (-1);
1033*5094Slling 		}
1034*5094Slling 
1035*5094Slling 		*num <<= shift;
1036*5094Slling 	}
1037*5094Slling 
1038*5094Slling 	return (0);
1039*5094Slling }
1040*5094Slling 
1041*5094Slling /*
1042*5094Slling  * Given a propname=value nvpair to set, parse any numeric properties
1043*5094Slling  * (index, boolean, etc) if they are specified as strings and add the
1044*5094Slling  * resulting nvpair to the returned nvlist.
1045*5094Slling  *
1046*5094Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1047*5094Slling  * We want the user to be able to ignore this fact and specify properties
1048*5094Slling  * as native values (numbers, for example) or as strings (to simplify
1049*5094Slling  * command line utilities).  This also handles converting index types
1050*5094Slling  * (compression, checksum, etc) from strings to their on-disk index.
1051*5094Slling  */
1052*5094Slling int
1053*5094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1054*5094Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1055*5094Slling     const char *errbuf)
10564543Smarks {
1057*5094Slling 	data_type_t datatype = nvpair_type(elem);
1058*5094Slling 	zprop_type_t proptype;
1059*5094Slling 	const char *propname;
1060*5094Slling 	char *value;
1061*5094Slling 	boolean_t isnone = B_FALSE;
1062*5094Slling 
1063*5094Slling 	if (type == ZFS_TYPE_POOL) {
1064*5094Slling 		proptype = zpool_prop_get_type(prop);
1065*5094Slling 		propname = zpool_prop_to_name(prop);
1066*5094Slling 	} else {
1067*5094Slling 		proptype = zfs_prop_get_type(prop);
1068*5094Slling 		propname = zfs_prop_to_name(prop);
1069*5094Slling 	}
1070*5094Slling 
1071*5094Slling 	/*
1072*5094Slling 	 * Convert any properties to the internal DSL value types.
1073*5094Slling 	 */
1074*5094Slling 	*svalp = NULL;
1075*5094Slling 	*ivalp = 0;
1076*5094Slling 
1077*5094Slling 	switch (proptype) {
1078*5094Slling 	case PROP_TYPE_STRING:
1079*5094Slling 		if (datatype != DATA_TYPE_STRING) {
1080*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1081*5094Slling 			    "'%s' must be a string"), nvpair_name(elem));
1082*5094Slling 			goto error;
1083*5094Slling 		}
1084*5094Slling 		(void) nvpair_value_string(elem, svalp);
1085*5094Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1086*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1087*5094Slling 			    "'%s' is too long"), nvpair_name(elem));
1088*5094Slling 			goto error;
1089*5094Slling 		}
1090*5094Slling 		break;
1091*5094Slling 
1092*5094Slling 	case PROP_TYPE_NUMBER:
1093*5094Slling 		if (datatype == DATA_TYPE_STRING) {
1094*5094Slling 			(void) nvpair_value_string(elem, &value);
1095*5094Slling 			if (strcmp(value, "none") == 0) {
1096*5094Slling 				isnone = B_TRUE;
1097*5094Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1098*5094Slling 			    != 0) {
1099*5094Slling 				goto error;
1100*5094Slling 			}
1101*5094Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1102*5094Slling 			(void) nvpair_value_uint64(elem, ivalp);
1103*5094Slling 		} else {
1104*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1105*5094Slling 			    "'%s' must be a number"), nvpair_name(elem));
1106*5094Slling 			goto error;
1107*5094Slling 		}
1108*5094Slling 
1109*5094Slling 		/*
1110*5094Slling 		 * Quota special: force 'none' and don't allow 0.
1111*5094Slling 		 */
1112*5094Slling 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 &&
1113*5094Slling 		    !isnone && prop == ZFS_PROP_QUOTA) {
1114*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115*5094Slling 			    "use 'none' to disable quota"));
1116*5094Slling 			goto error;
1117*5094Slling 		}
1118*5094Slling 		break;
1119*5094Slling 
1120*5094Slling 	case PROP_TYPE_INDEX:
1121*5094Slling 		if (datatype != DATA_TYPE_STRING) {
1122*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1123*5094Slling 			    "'%s' must be a string"), nvpair_name(elem));
1124*5094Slling 			goto error;
1125*5094Slling 		}
1126*5094Slling 
1127*5094Slling 		(void) nvpair_value_string(elem, &value);
1128*5094Slling 
1129*5094Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1130*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131*5094Slling 			    "'%s' must be one of '%s'"), propname,
1132*5094Slling 			    zprop_values(prop, type));
1133*5094Slling 			goto error;
1134*5094Slling 		}
1135*5094Slling 		break;
1136*5094Slling 
1137*5094Slling 	default:
1138*5094Slling 		abort();
1139*5094Slling 	}
11404543Smarks 
1141*5094Slling 	/*
1142*5094Slling 	 * Add the result to our return set of properties.
1143*5094Slling 	 */
1144*5094Slling 	if (*svalp != NULL) {
1145*5094Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1146*5094Slling 			(void) no_memory(hdl);
1147*5094Slling 			return (-1);
1148*5094Slling 		}
1149*5094Slling 	} else {
1150*5094Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1151*5094Slling 			(void) no_memory(hdl);
1152*5094Slling 			return (-1);
1153*5094Slling 		}
1154*5094Slling 	}
1155*5094Slling 
1156*5094Slling 	return (0);
1157*5094Slling error:
1158*5094Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1159*5094Slling 	return (-1);
1160*5094Slling }
1161*5094Slling 
1162*5094Slling /*
1163*5094Slling  * Given a comma-separated list of properties, construct a property list
1164*5094Slling  * containing both user-defined and native properties.  This function will
1165*5094Slling  * return a NULL list if 'all' is specified, which can later be expanded
1166*5094Slling  * by zprop_expand_list().
1167*5094Slling  */
1168*5094Slling int
1169*5094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1170*5094Slling     zfs_type_t type)
1171*5094Slling {
1172*5094Slling 	size_t len;
1173*5094Slling 	char *s, *p;
1174*5094Slling 	char c;
1175*5094Slling 	int prop;
1176*5094Slling 	zprop_list_t *entry;
1177*5094Slling 	zprop_list_t **last;
1178*5094Slling 
1179*5094Slling 	*listp = NULL;
1180*5094Slling 	last = listp;
1181*5094Slling 
1182*5094Slling 	/*
1183*5094Slling 	 * If 'all' is specified, return a NULL list.
1184*5094Slling 	 */
1185*5094Slling 	if (strcmp(props, "all") == 0)
1186*5094Slling 		return (0);
1187*5094Slling 
1188*5094Slling 	/*
1189*5094Slling 	 * If no props were specified, return an error.
1190*5094Slling 	 */
1191*5094Slling 	if (props[0] == '\0') {
1192*5094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1193*5094Slling 		    "no properties specified"));
1194*5094Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1195*5094Slling 		    "bad property list")));
11964543Smarks 	}
1197*5094Slling 
1198*5094Slling 	/*
1199*5094Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1200*5094Slling 	 * aliases makes this more effort than it's worth.
1201*5094Slling 	 */
1202*5094Slling 	s = props;
1203*5094Slling 	while (*s != '\0') {
1204*5094Slling 		if ((p = strchr(s, ',')) == NULL) {
1205*5094Slling 			len = strlen(s);
1206*5094Slling 			p = s + len;
1207*5094Slling 		} else {
1208*5094Slling 			len = p - s;
1209*5094Slling 		}
1210*5094Slling 
1211*5094Slling 		/*
1212*5094Slling 		 * Check for empty options.
1213*5094Slling 		 */
1214*5094Slling 		if (len == 0) {
1215*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1216*5094Slling 			    "empty property name"));
1217*5094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1218*5094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1219*5094Slling 		}
1220*5094Slling 
1221*5094Slling 		/*
1222*5094Slling 		 * Check all regular property names.
1223*5094Slling 		 */
1224*5094Slling 		c = s[len];
1225*5094Slling 		s[len] = '\0';
1226*5094Slling 		prop = zprop_name_to_prop(s, type);
1227*5094Slling 
1228*5094Slling 		if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1229*5094Slling 			prop = ZPROP_INVAL;
1230*5094Slling 
1231*5094Slling 		/*
1232*5094Slling 		 * When no property table entry can be found, return failure if
1233*5094Slling 		 * this is a pool property or if this isn't a user-defined
1234*5094Slling 		 * dataset property,
1235*5094Slling 		 */
1236*5094Slling 		if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1237*5094Slling 		    !zfs_prop_user(s))) {
1238*5094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1239*5094Slling 			    "invalid property '%s'"), s);
1240*5094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1241*5094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1242*5094Slling 		}
1243*5094Slling 
1244*5094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1245*5094Slling 			return (-1);
1246*5094Slling 
1247*5094Slling 		entry->pl_prop = prop;
1248*5094Slling 		if (prop == ZPROP_INVAL) {
1249*5094Slling 			if ((entry->pl_user_prop = zfs_strdup(hdl, s))
1250*5094Slling 			    == NULL) {
1251*5094Slling 				free(entry);
1252*5094Slling 				return (-1);
1253*5094Slling 			}
1254*5094Slling 			entry->pl_width = strlen(s);
1255*5094Slling 		} else {
1256*5094Slling 			entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1257*5094Slling 			    type);
1258*5094Slling 		}
1259*5094Slling 
1260*5094Slling 		*last = entry;
1261*5094Slling 		last = &entry->pl_next;
1262*5094Slling 
1263*5094Slling 		s = p;
1264*5094Slling 		if (c == ',')
1265*5094Slling 			s++;
1266*5094Slling 	}
1267*5094Slling 
1268*5094Slling 	return (0);
1269*5094Slling }
1270*5094Slling 
1271*5094Slling void
1272*5094Slling zprop_free_list(zprop_list_t *pl)
1273*5094Slling {
1274*5094Slling 	zprop_list_t *next;
12754543Smarks 
1276*5094Slling 	while (pl != NULL) {
1277*5094Slling 		next = pl->pl_next;
1278*5094Slling 		free(pl->pl_user_prop);
1279*5094Slling 		free(pl);
1280*5094Slling 		pl = next;
1281*5094Slling 	}
1282*5094Slling }
1283*5094Slling 
1284*5094Slling typedef struct expand_data {
1285*5094Slling 	zprop_list_t	**last;
1286*5094Slling 	libzfs_handle_t	*hdl;
1287*5094Slling 	zfs_type_t type;
1288*5094Slling } expand_data_t;
1289*5094Slling 
1290*5094Slling int
1291*5094Slling zprop_expand_list_cb(int prop, void *cb)
1292*5094Slling {
1293*5094Slling 	zprop_list_t *entry;
1294*5094Slling 	expand_data_t *edp = cb;
1295*5094Slling 
1296*5094Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1297*5094Slling 		return (ZPROP_INVAL);
1298*5094Slling 
1299*5094Slling 	entry->pl_prop = prop;
1300*5094Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1301*5094Slling 	entry->pl_all = B_TRUE;
1302*5094Slling 
1303*5094Slling 	*(edp->last) = entry;
1304*5094Slling 	edp->last = &entry->pl_next;
1305*5094Slling 
1306*5094Slling 	return (ZPROP_CONT);
13074543Smarks }
1308*5094Slling 
1309*5094Slling int
1310*5094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1311*5094Slling {
1312*5094Slling 	zprop_list_t *entry;
1313*5094Slling 	zprop_list_t **last;
1314*5094Slling 	expand_data_t exp;
1315*5094Slling 
1316*5094Slling 	if (*plp == NULL) {
1317*5094Slling 		/*
1318*5094Slling 		 * If this is the very first time we've been called for an 'all'
1319*5094Slling 		 * specification, expand the list to include all native
1320*5094Slling 		 * properties.
1321*5094Slling 		 */
1322*5094Slling 		last = plp;
1323*5094Slling 
1324*5094Slling 		exp.last = last;
1325*5094Slling 		exp.hdl = hdl;
1326*5094Slling 		exp.type = type;
1327*5094Slling 
1328*5094Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1329*5094Slling 		    B_FALSE, type) == ZPROP_INVAL)
1330*5094Slling 			return (-1);
1331*5094Slling 
1332*5094Slling 		/*
1333*5094Slling 		 * Add 'name' to the beginning of the list, which is handled
1334*5094Slling 		 * specially.
1335*5094Slling 		 */
1336*5094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1337*5094Slling 			return (-1);
1338*5094Slling 
1339*5094Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1340*5094Slling 		    ZFS_PROP_NAME;
1341*5094Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1342*5094Slling 		    &entry->pl_fixed, type);
1343*5094Slling 		entry->pl_all = B_TRUE;
1344*5094Slling 		entry->pl_next = *plp;
1345*5094Slling 		*plp = entry;
1346*5094Slling 	}
1347*5094Slling 	return (0);
1348*5094Slling }
1349*5094Slling 
1350*5094Slling int
1351*5094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1352*5094Slling     zfs_type_t type)
1353*5094Slling {
1354*5094Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1355*5094Slling }
1356