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>
405094Slling #include <ctype.h>
415094Slling #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"));
1395331Samw 	case EZFS_UNSHARESMBFAILED:
1405331Samw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
1415331Samw 	case EZFS_SHARESMBFAILED:
1425331Samw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
1434543Smarks 	case EZFS_ISCSISVCUNAVAIL:
1444543Smarks 		return (dgettext(TEXT_DOMAIN,
1454543Smarks 		    "iscsitgt service need to be enabled by "
1464543Smarks 		    "a privileged user"));
1472082Seschrock 	case EZFS_DEVLINKS:
1482082Seschrock 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
1492082Seschrock 	case EZFS_PERM:
1502082Seschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
1512082Seschrock 	case EZFS_NOSPC:
1522082Seschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
1532082Seschrock 	case EZFS_IO:
1542082Seschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
1552082Seschrock 	case EZFS_INTR:
1562082Seschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
1572082Seschrock 	case EZFS_ISSPARE:
1582082Seschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
1592082Seschrock 		    "spare"));
1602082Seschrock 	case EZFS_INVALCONFIG:
1612082Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1622474Seschrock 	case EZFS_RECURSIVE:
1632474Seschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
1642926Sek110237 	case EZFS_NOHISTORY:
1652926Sek110237 		return (dgettext(TEXT_DOMAIN, "no history available"));
1663126Sahl 	case EZFS_UNSHAREISCSIFAILED:
1673126Sahl 		return (dgettext(TEXT_DOMAIN,
1683126Sahl 		    "iscsitgtd failed request to unshare"));
1693126Sahl 	case EZFS_SHAREISCSIFAILED:
1703126Sahl 		return (dgettext(TEXT_DOMAIN,
1713126Sahl 		    "iscsitgtd failed request to share"));
1723912Slling 	case EZFS_POOLPROPS:
1733912Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
1743912Slling 		    "pool properties"));
1753912Slling 	case EZFS_POOL_NOTSUP:
1763912Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
1773912Slling 		    "on this type of pool"));
1783912Slling 	case EZFS_POOL_INVALARG:
1793912Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
1803912Slling 		    "this pool operation"));
1813978Smmusante 	case EZFS_NAMETOOLONG:
1823978Smmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1834276Staylor 	case EZFS_OPENFAILED:
1844276Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1854276Staylor 	case EZFS_NOCAP:
1864276Staylor 		return (dgettext(TEXT_DOMAIN,
1874276Staylor 		    "disk capacity information could not be retrieved"));
1884276Staylor 	case EZFS_LABELFAILED:
1894276Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
1904543Smarks 	case EZFS_BADWHO:
1914543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
1924543Smarks 	case EZFS_BADPERM:
1934543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
1944543Smarks 	case EZFS_BADPERMSET:
1954543Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
1964543Smarks 	case EZFS_NODELEGATION:
1974543Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
1984543Smarks 		    "disabled on pool"));
1994543Smarks 	case EZFS_PERMRDONLY:
2004543Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
2014543Smarks 		    " modified"));
2025363Seschrock 	case EZFS_BADCACHE:
2035363Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
2042082Seschrock 	case EZFS_UNKNOWN:
2052082Seschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
2062082Seschrock 	default:
2072500Seschrock 		assert(hdl->libzfs_error == 0);
2082500Seschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
2092082Seschrock 	}
2102082Seschrock }
2112082Seschrock 
2122082Seschrock /*PRINTFLIKE2*/
213789Sahrens void
2142082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
215789Sahrens {
216789Sahrens 	va_list ap;
217789Sahrens 
218789Sahrens 	va_start(ap, fmt);
219789Sahrens 
2202082Seschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
2212082Seschrock 	    fmt, ap);
2222082Seschrock 	hdl->libzfs_desc_active = 1;
223789Sahrens 
224789Sahrens 	va_end(ap);
225789Sahrens }
226789Sahrens 
2272082Seschrock static void
2282082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
2292082Seschrock {
2302082Seschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
2312082Seschrock 	    fmt, ap);
2322082Seschrock 	hdl->libzfs_error = error;
2332082Seschrock 
2342082Seschrock 	if (hdl->libzfs_desc_active)
2352082Seschrock 		hdl->libzfs_desc_active = 0;
2362082Seschrock 	else
2372082Seschrock 		hdl->libzfs_desc[0] = '\0';
2382082Seschrock 
2392082Seschrock 	if (hdl->libzfs_printerr) {
2402082Seschrock 		if (error == EZFS_UNKNOWN) {
2412082Seschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
2422082Seschrock 			    "error: %s\n"), libzfs_error_description(hdl));
2432082Seschrock 			abort();
2442082Seschrock 		}
2452082Seschrock 
2462082Seschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
2473912Slling 		    libzfs_error_description(hdl));
2482082Seschrock 		if (error == EZFS_NOMEM)
2492082Seschrock 			exit(1);
2502082Seschrock 	}
2512082Seschrock }
2522082Seschrock 
2533237Slling int
2543237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
2553237Slling {
2563237Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
2573237Slling }
2583237Slling 
2592082Seschrock /*PRINTFLIKE3*/
2602082Seschrock int
2613237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
2622082Seschrock {
2632082Seschrock 	va_list ap;
2642082Seschrock 
2652082Seschrock 	va_start(ap, fmt);
2662082Seschrock 
2672082Seschrock 	zfs_verror(hdl, error, fmt, ap);
2682082Seschrock 
2692082Seschrock 	va_end(ap);
2702082Seschrock 
2712082Seschrock 	return (-1);
2722082Seschrock }
2732082Seschrock 
2742082Seschrock static int
2752082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
2762082Seschrock     va_list ap)
2772082Seschrock {
2782082Seschrock 	switch (error) {
2792082Seschrock 	case EPERM:
2802082Seschrock 	case EACCES:
2812082Seschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
2822082Seschrock 		return (-1);
2832082Seschrock 
2844543Smarks 	case ECANCELED:
2854543Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
2864543Smarks 		return (-1);
2874543Smarks 
2882082Seschrock 	case EIO:
2892082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
2902082Seschrock 		return (-1);
2912082Seschrock 
2922082Seschrock 	case EINTR:
2932082Seschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
2942082Seschrock 		return (-1);
2952082Seschrock 	}
2962082Seschrock 
2972082Seschrock 	return (0);
2982082Seschrock }
2992082Seschrock 
3003237Slling int
3013237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3023237Slling {
3033237Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
3043237Slling }
3053237Slling 
3062082Seschrock /*PRINTFLIKE3*/
3072082Seschrock int
3083237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
309789Sahrens {
310789Sahrens 	va_list ap;
311789Sahrens 
312789Sahrens 	va_start(ap, fmt);
313789Sahrens 
3142082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3152082Seschrock 		va_end(ap);
3162082Seschrock 		return (-1);
3172082Seschrock 	}
3182082Seschrock 
3192082Seschrock 
3202082Seschrock 	switch (error) {
3212082Seschrock 	case ENXIO:
3222082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3232082Seschrock 		break;
3242082Seschrock 
3252082Seschrock 	case ENOENT:
3262082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3272082Seschrock 		    "dataset does not exist"));
3282082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3292082Seschrock 		break;
3302082Seschrock 
3312082Seschrock 	case ENOSPC:
3322082Seschrock 	case EDQUOT:
3332082Seschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
3342082Seschrock 		return (-1);
3352082Seschrock 
3362082Seschrock 	case EEXIST:
3372082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3382082Seschrock 		    "dataset already exists"));
3392082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3402082Seschrock 		break;
3412082Seschrock 
3422082Seschrock 	case EBUSY:
3432082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3442082Seschrock 		    "dataset is busy"));
3452082Seschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
3462082Seschrock 		break;
3474543Smarks 	case EROFS:
3484543Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3494543Smarks 		    "snapshot permissions cannot be modified"));
3504543Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
3514543Smarks 		break;
3523978Smmusante 	case ENAMETOOLONG:
3533978Smmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
3543978Smmusante 		break;
3552082Seschrock 	default:
3562082Seschrock 		zfs_error_aux(hdl, strerror(errno));
3572082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
3582082Seschrock 		break;
359789Sahrens 	}
360789Sahrens 
361789Sahrens 	va_end(ap);
3622082Seschrock 	return (-1);
363789Sahrens }
364789Sahrens 
3653237Slling int
3663237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3673237Slling {
3683237Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
3693237Slling }
3703237Slling 
3712082Seschrock /*PRINTFLIKE3*/
3722082Seschrock int
3733237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
374789Sahrens {
3752082Seschrock 	va_list ap;
3762082Seschrock 
3772082Seschrock 	va_start(ap, fmt);
3782082Seschrock 
3792082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3802082Seschrock 		va_end(ap);
3812082Seschrock 		return (-1);
3822082Seschrock 	}
3832082Seschrock 
3842082Seschrock 	switch (error) {
3852082Seschrock 	case ENODEV:
3862082Seschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
3872082Seschrock 		break;
3882082Seschrock 
3892082Seschrock 	case ENOENT:
3903912Slling 		zfs_error_aux(hdl,
3913912Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
3922082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3932082Seschrock 		break;
3942082Seschrock 
3952082Seschrock 	case EEXIST:
3962082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3972082Seschrock 		    "pool already exists"));
3982082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3992082Seschrock 		break;
4002082Seschrock 
4012082Seschrock 	case EBUSY:
4022082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
4032082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
4042082Seschrock 		break;
4052082Seschrock 
4062082Seschrock 	case ENXIO:
4072082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4082082Seschrock 		    "one or more devices is currently unavailable"));
4092082Seschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
4102082Seschrock 		break;
4112082Seschrock 
4122082Seschrock 	case ENAMETOOLONG:
4132082Seschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
4142082Seschrock 		break;
4152082Seschrock 
4163912Slling 	case ENOTSUP:
4173912Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
4183912Slling 		break;
4193912Slling 
4203912Slling 	case EINVAL:
4213912Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
4223912Slling 		break;
4233912Slling 
4244543Smarks 	case ENOSPC:
4254543Smarks 	case EDQUOT:
4264543Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
4274543Smarks 		return (-1);
4284543Smarks 
4292082Seschrock 	default:
4302082Seschrock 		zfs_error_aux(hdl, strerror(error));
4312082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
4322082Seschrock 	}
4332082Seschrock 
4342082Seschrock 	va_end(ap);
4352082Seschrock 	return (-1);
436789Sahrens }
437789Sahrens 
438789Sahrens /*
439789Sahrens  * Display an out of memory error message and abort the current program.
440789Sahrens  */
4412082Seschrock int
4422082Seschrock no_memory(libzfs_handle_t *hdl)
443789Sahrens {
4442082Seschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
445789Sahrens }
446789Sahrens 
447789Sahrens /*
448789Sahrens  * A safe form of malloc() which will die if the allocation fails.
449789Sahrens  */
450789Sahrens void *
4512082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
452789Sahrens {
453789Sahrens 	void *data;
454789Sahrens 
455789Sahrens 	if ((data = calloc(1, size)) == NULL)
4562082Seschrock 		(void) no_memory(hdl);
457789Sahrens 
458789Sahrens 	return (data);
459789Sahrens }
460789Sahrens 
461789Sahrens /*
4622676Seschrock  * A safe form of realloc(), which also zeroes newly allocated space.
4632676Seschrock  */
4642676Seschrock void *
4652676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
4662676Seschrock {
4672676Seschrock 	void *ret;
4682676Seschrock 
4692676Seschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
4702676Seschrock 		(void) no_memory(hdl);
4712676Seschrock 		free(ptr);
4722676Seschrock 		return (NULL);
4732676Seschrock 	}
4742676Seschrock 
4752676Seschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
4762676Seschrock 	return (ret);
4772676Seschrock }
4782676Seschrock 
4792676Seschrock /*
480789Sahrens  * A safe form of strdup() which will die if the allocation fails.
481789Sahrens  */
482789Sahrens char *
4832082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
484789Sahrens {
485789Sahrens 	char *ret;
486789Sahrens 
487789Sahrens 	if ((ret = strdup(str)) == NULL)
4882082Seschrock 		(void) no_memory(hdl);
489789Sahrens 
490789Sahrens 	return (ret);
491789Sahrens }
492789Sahrens 
493789Sahrens /*
494789Sahrens  * Convert a number to an appropriately human-readable output.
495789Sahrens  */
496789Sahrens void
497789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
498789Sahrens {
499789Sahrens 	uint64_t n = num;
500789Sahrens 	int index = 0;
501789Sahrens 	char u;
502789Sahrens 
503789Sahrens 	while (n >= 1024) {
5041162Seschrock 		n /= 1024;
505789Sahrens 		index++;
506789Sahrens 	}
507789Sahrens 
508789Sahrens 	u = " KMGTPE"[index];
509789Sahrens 
5101162Seschrock 	if (index == 0) {
511789Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5121162Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5131162Seschrock 		/*
5141162Seschrock 		 * If this is an even multiple of the base, always display
5151162Seschrock 		 * without any decimal precision.
5161162Seschrock 		 */
517789Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5181162Seschrock 	} else {
5191162Seschrock 		/*
5201162Seschrock 		 * We want to choose a precision that reflects the best choice
5211162Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5221162Seschrock 		 * we have numbers that are very close to an order of magnitude.
5231162Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5241162Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5251162Seschrock 		 * develop some complex heuristics for this, but it's much
5261162Seschrock 		 * easier just to try each combination in turn.
5271162Seschrock 		 */
5281162Seschrock 		int i;
5291162Seschrock 		for (i = 2; i >= 0; i--) {
5304451Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5314451Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5321162Seschrock 				break;
5331162Seschrock 		}
5341162Seschrock 	}
535789Sahrens }
5362082Seschrock 
5372082Seschrock void
5382082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
5392082Seschrock {
5402082Seschrock 	hdl->libzfs_printerr = printerr;
5412082Seschrock }
5422082Seschrock 
5432082Seschrock libzfs_handle_t *
5442082Seschrock libzfs_init(void)
5452082Seschrock {
5462082Seschrock 	libzfs_handle_t *hdl;
5472082Seschrock 
5482082Seschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
5492082Seschrock 		return (NULL);
5502082Seschrock 	}
5512082Seschrock 
5522500Seschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
5532082Seschrock 		free(hdl);
5542082Seschrock 		return (NULL);
5552082Seschrock 	}
5562082Seschrock 
5572082Seschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
5582082Seschrock 		(void) close(hdl->libzfs_fd);
5592082Seschrock 		free(hdl);
5602082Seschrock 		return (NULL);
5612082Seschrock 	}
5622082Seschrock 
5632082Seschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
5642082Seschrock 
5654787Sahrens 	zfs_prop_init();
5665094Slling 	zpool_prop_init();
5674787Sahrens 
5682082Seschrock 	return (hdl);
5692082Seschrock }
5702082Seschrock 
5712082Seschrock void
5722082Seschrock libzfs_fini(libzfs_handle_t *hdl)
5732082Seschrock {
5742082Seschrock 	(void) close(hdl->libzfs_fd);
5752082Seschrock 	if (hdl->libzfs_mnttab)
5762082Seschrock 		(void) fclose(hdl->libzfs_mnttab);
5772082Seschrock 	if (hdl->libzfs_sharetab)
5782082Seschrock 		(void) fclose(hdl->libzfs_sharetab);
5794180Sdougm 	zfs_uninit_libshare(hdl);
5804543Smarks 	if (hdl->libzfs_log_str)
5814543Smarks 		(void) free(hdl->libzfs_log_str);
5822082Seschrock 	namespace_clear(hdl);
5832082Seschrock 	free(hdl);
5842082Seschrock }
5852082Seschrock 
5862082Seschrock libzfs_handle_t *
5872082Seschrock zpool_get_handle(zpool_handle_t *zhp)
5882082Seschrock {
5892082Seschrock 	return (zhp->zpool_hdl);
5902082Seschrock }
5912082Seschrock 
5922082Seschrock libzfs_handle_t *
5932082Seschrock zfs_get_handle(zfs_handle_t *zhp)
5942082Seschrock {
5952082Seschrock 	return (zhp->zfs_hdl);
5962082Seschrock }
5972676Seschrock 
5982676Seschrock /*
5993635Sck153898  * Given a name, determine whether or not it's a valid path
6003635Sck153898  * (starts with '/' or "./").  If so, walk the mnttab trying
6013635Sck153898  * to match the device number.  If not, treat the path as an
6023635Sck153898  * fs/vol/snap name.
6033635Sck153898  */
6043635Sck153898 zfs_handle_t *
6053635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6063635Sck153898 {
6073635Sck153898 	struct stat64 statbuf;
6083635Sck153898 	struct extmnttab entry;
6093635Sck153898 	int ret;
6103635Sck153898 
6113635Sck153898 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6123635Sck153898 		/*
6133635Sck153898 		 * It's not a valid path, assume it's a name of type 'argtype'.
6143635Sck153898 		 */
6153635Sck153898 		return (zfs_open(hdl, path, argtype));
6163635Sck153898 	}
6173635Sck153898 
6183635Sck153898 	if (stat64(path, &statbuf) != 0) {
6193635Sck153898 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6203635Sck153898 		return (NULL);
6213635Sck153898 	}
6223635Sck153898 
6233635Sck153898 	rewind(hdl->libzfs_mnttab);
6243635Sck153898 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6253635Sck153898 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6263635Sck153898 		    statbuf.st_dev) {
6273635Sck153898 			break;
6283635Sck153898 		}
6293635Sck153898 	}
6303635Sck153898 	if (ret != 0) {
6313635Sck153898 		return (NULL);
6323635Sck153898 	}
6333635Sck153898 
6343635Sck153898 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6353635Sck153898 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6363635Sck153898 		    path);
6373635Sck153898 		return (NULL);
6383635Sck153898 	}
6393635Sck153898 
6403635Sck153898 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6413635Sck153898 }
6423635Sck153898 
6433635Sck153898 /*
6442676Seschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
6452676Seschrock  * an ioctl().
6462676Seschrock  */
6472676Seschrock int
6482676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
6492676Seschrock {
6502676Seschrock 	if (len == 0)
6512885Sahrens 		len = 2048;
6522676Seschrock 	zc->zc_nvlist_dst_size = len;
6532676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
6542676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
6552676Seschrock 		return (-1);
6562676Seschrock 
6572676Seschrock 	return (0);
6582676Seschrock }
6592676Seschrock 
6602676Seschrock /*
6612676Seschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
6622676Seschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
6632676Seschrock  * filled in by the kernel to indicate the actual required size.
6642676Seschrock  */
6652676Seschrock int
6662676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
6672676Seschrock {
6682676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
6692676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
6702676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
6712676Seschrock 	    == NULL)
6722676Seschrock 		return (-1);
6732676Seschrock 
6742676Seschrock 	return (0);
6752676Seschrock }
6762676Seschrock 
6772676Seschrock /*
6782885Sahrens  * Called to free the src and dst nvlists stored in the command structure.
6792676Seschrock  */
6802676Seschrock void
6812676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc)
6822676Seschrock {
6835094Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
6842676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
6852676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
6862676Seschrock }
6872676Seschrock 
6885094Slling static int
6895094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
6905094Slling     nvlist_t *nvl)
6912676Seschrock {
6922676Seschrock 	char *packed;
6932676Seschrock 	size_t len;
6942676Seschrock 
6952676Seschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
6962676Seschrock 
6972676Seschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
6982676Seschrock 		return (-1);
6992676Seschrock 
7002676Seschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
7012676Seschrock 
7025094Slling 	*outnv = (uint64_t)(uintptr_t)packed;
7035094Slling 	*outlen = len;
7045094Slling 
7055094Slling 	return (0);
7065094Slling }
7072676Seschrock 
7085094Slling int
7095094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7105094Slling {
7115094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
7125094Slling 	    &zc->zc_nvlist_conf_size, nvl));
7135094Slling }
7145094Slling 
7155094Slling int
7165094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7175094Slling {
7185094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
7195094Slling 	    &zc->zc_nvlist_src_size, nvl));
7202676Seschrock }
7212676Seschrock 
7222676Seschrock /*
7232676Seschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
7242676Seschrock  */
7252676Seschrock int
7262676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
7272676Seschrock {
7282676Seschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
7292676Seschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
7302676Seschrock 		return (no_memory(hdl));
7312676Seschrock 
7322676Seschrock 	return (0);
7332676Seschrock }
7343912Slling 
7355094Slling int
7365094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
7375094Slling {
7385094Slling 	int error;
7395094Slling 
7405094Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
7415094Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
7425094Slling 	if (hdl->libzfs_log_str) {
7435094Slling 		free(hdl->libzfs_log_str);
7445094Slling 		hdl->libzfs_log_str = NULL;
7455094Slling 	}
7465094Slling 	zc->zc_history = 0;
7475094Slling 
7485094Slling 	return (error);
7495094Slling }
7505094Slling 
7515094Slling /*
7525094Slling  * ================================================================
7535094Slling  * API shared by zfs and zpool property management
7545094Slling  * ================================================================
7555094Slling  */
7565094Slling 
7573912Slling static void
7585094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
7593912Slling {
7605094Slling 	zprop_list_t *pl = cbp->cb_proplist;
7613912Slling 	int i;
7623912Slling 	char *title;
7633912Slling 	size_t len;
7643912Slling 
7653912Slling 	cbp->cb_first = B_FALSE;
7663912Slling 	if (cbp->cb_scripted)
7673912Slling 		return;
7683912Slling 
7693912Slling 	/*
7703912Slling 	 * Start with the length of the column headers.
7713912Slling 	 */
7723912Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
7733912Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
7743912Slling 	    "PROPERTY"));
7753912Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
7763912Slling 	    "VALUE"));
7773912Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
7783912Slling 	    "SOURCE"));
7793912Slling 
7803912Slling 	/*
7813912Slling 	 * Go through and calculate the widths for each column.  For the
7823912Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
7833912Slling 	 * inheriting from the longest name.  This is acceptable because in the
7843912Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
7853912Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
7863912Slling 	 * if the name of the property is much longer the any values we find.
7873912Slling 	 */
7883912Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7893912Slling 		/*
7903912Slling 		 * 'PROPERTY' column
7913912Slling 		 */
7925094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
7935094Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
7945094Slling 			    zpool_prop_to_name(pl->pl_prop) :
7955094Slling 			    zfs_prop_to_name(pl->pl_prop);
7965094Slling 
7975094Slling 			len = strlen(propname);
7983912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
7993912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8003912Slling 		} else {
8013912Slling 			len = strlen(pl->pl_user_prop);
8023912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8033912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8043912Slling 		}
8053912Slling 
8063912Slling 		/*
8073912Slling 		 * 'VALUE' column
8083912Slling 		 */
8093912Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
8103912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
8113912Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
8123912Slling 
8133912Slling 		/*
8143912Slling 		 * 'NAME' and 'SOURCE' columns
8153912Slling 		 */
8165094Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
8175094Slling 		    ZFS_PROP_NAME) &&
8183912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
8193912Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
8203912Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
8213912Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
8223912Slling 		}
8233912Slling 	}
8243912Slling 
8253912Slling 	/*
8263912Slling 	 * Now go through and print the headers.
8273912Slling 	 */
8283912Slling 	for (i = 0; i < 4; i++) {
8293912Slling 		switch (cbp->cb_columns[i]) {
8303912Slling 		case GET_COL_NAME:
8313912Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
8323912Slling 			break;
8333912Slling 		case GET_COL_PROPERTY:
8343912Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
8353912Slling 			break;
8363912Slling 		case GET_COL_VALUE:
8373912Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
8383912Slling 			break;
8393912Slling 		case GET_COL_SOURCE:
8403912Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
8413912Slling 			break;
8423912Slling 		default:
8433912Slling 			title = NULL;
8443912Slling 		}
8453912Slling 
8463912Slling 		if (title != NULL) {
8473912Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
8483912Slling 				(void) printf("%s", title);
8493912Slling 			else
8503912Slling 				(void) printf("%-*s  ",
8513912Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
8523912Slling 				    title);
8533912Slling 		}
8543912Slling 	}
8553912Slling 	(void) printf("\n");
8563912Slling }
8573912Slling 
8583912Slling /*
8593912Slling  * Display a single line of output, according to the settings in the callback
8603912Slling  * structure.
8613912Slling  */
8623912Slling void
8635094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
8645094Slling     const char *propname, const char *value, zprop_source_t sourcetype,
8653912Slling     const char *source)
8663912Slling {
8673912Slling 	int i;
8683912Slling 	const char *str;
8693912Slling 	char buf[128];
8703912Slling 
8713912Slling 	/*
8723912Slling 	 * Ignore those source types that the user has chosen to ignore.
8733912Slling 	 */
8743912Slling 	if ((sourcetype & cbp->cb_sources) == 0)
8753912Slling 		return;
8763912Slling 
8773912Slling 	if (cbp->cb_first)
8785094Slling 		zprop_print_headers(cbp, cbp->cb_type);
8793912Slling 
8803912Slling 	for (i = 0; i < 4; i++) {
8813912Slling 		switch (cbp->cb_columns[i]) {
8823912Slling 		case GET_COL_NAME:
8833912Slling 			str = name;
8843912Slling 			break;
8853912Slling 
8863912Slling 		case GET_COL_PROPERTY:
8873912Slling 			str = propname;
8883912Slling 			break;
8893912Slling 
8903912Slling 		case GET_COL_VALUE:
8913912Slling 			str = value;
8923912Slling 			break;
8933912Slling 
8943912Slling 		case GET_COL_SOURCE:
8953912Slling 			switch (sourcetype) {
8965094Slling 			case ZPROP_SRC_NONE:
8973912Slling 				str = "-";
8983912Slling 				break;
8993912Slling 
9005094Slling 			case ZPROP_SRC_DEFAULT:
9013912Slling 				str = "default";
9023912Slling 				break;
9033912Slling 
9045094Slling 			case ZPROP_SRC_LOCAL:
9053912Slling 				str = "local";
9063912Slling 				break;
9073912Slling 
9085094Slling 			case ZPROP_SRC_TEMPORARY:
9093912Slling 				str = "temporary";
9103912Slling 				break;
9113912Slling 
9125094Slling 			case ZPROP_SRC_INHERITED:
9133912Slling 				(void) snprintf(buf, sizeof (buf),
9143912Slling 				    "inherited from %s", source);
9153912Slling 				str = buf;
9163912Slling 				break;
9173912Slling 			}
9183912Slling 			break;
9193912Slling 
9203912Slling 		default:
9213912Slling 			continue;
9223912Slling 		}
9233912Slling 
9243912Slling 		if (cbp->cb_columns[i + 1] == 0)
9253912Slling 			(void) printf("%s", str);
9263912Slling 		else if (cbp->cb_scripted)
9273912Slling 			(void) printf("%s\t", str);
9283912Slling 		else
9293912Slling 			(void) printf("%-*s  ",
9303912Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
9313912Slling 			    str);
9323912Slling 
9333912Slling 	}
9343912Slling 
9353912Slling 	(void) printf("\n");
9363912Slling }
9374543Smarks 
9385094Slling /*
9395094Slling  * Given a numeric suffix, convert the value into a number of bits that the
9405094Slling  * resulting value must be shifted.
9415094Slling  */
9425094Slling static int
9435094Slling str2shift(libzfs_handle_t *hdl, const char *buf)
9445094Slling {
9455094Slling 	const char *ends = "BKMGTPEZ";
9465094Slling 	int i;
9475094Slling 
9485094Slling 	if (buf[0] == '\0')
9495094Slling 		return (0);
9505094Slling 	for (i = 0; i < strlen(ends); i++) {
9515094Slling 		if (toupper(buf[0]) == ends[i])
9525094Slling 			break;
9535094Slling 	}
9545094Slling 	if (i == strlen(ends)) {
9555094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9565094Slling 		    "invalid numeric suffix '%s'"), buf);
9575094Slling 		return (-1);
9585094Slling 	}
9595094Slling 
9605094Slling 	/*
9615094Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
9625094Slling 	 * allow 'BB' - that's just weird.
9635094Slling 	 */
9645094Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
9655094Slling 	    toupper(buf[0]) != 'B'))
9665094Slling 		return (10*i);
9675094Slling 
9685094Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9695094Slling 	    "invalid numeric suffix '%s'"), buf);
9705094Slling 	return (-1);
9715094Slling }
9725094Slling 
9735094Slling /*
9745094Slling  * Convert a string of the form '100G' into a real number.  Used when setting
9755094Slling  * properties or creating a volume.  'buf' is used to place an extended error
9765094Slling  * message for the caller to use.
9775094Slling  */
9784543Smarks int
9795094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
9805094Slling {
9815094Slling 	char *end;
9825094Slling 	int shift;
9835094Slling 
9845094Slling 	*num = 0;
9855094Slling 
9865094Slling 	/* Check to see if this looks like a number.  */
9875094Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
9885094Slling 		if (hdl)
9895094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9905094Slling 			    "bad numeric value '%s'"), value);
9915094Slling 		return (-1);
9925094Slling 	}
9935094Slling 
9945094Slling 	/* Rely on stroll() to process the numeric portion.  */
9955094Slling 	errno = 0;
9965094Slling 	*num = strtoll(value, &end, 10);
9975094Slling 
9985094Slling 	/*
9995094Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
10005094Slling 	 * in a 64-bit value.
10015094Slling 	 */
10025094Slling 	if (errno == ERANGE) {
10035094Slling 		if (hdl)
10045094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10055094Slling 			    "numeric value is too large"));
10065094Slling 		return (-1);
10075094Slling 	}
10085094Slling 
10095094Slling 	/*
10105094Slling 	 * If we have a decimal value, then do the computation with floating
10115094Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
10125094Slling 	 */
10135094Slling 	if (*end == '.') {
10145094Slling 		double fval = strtod(value, &end);
10155094Slling 
10165094Slling 		if ((shift = str2shift(hdl, end)) == -1)
10175094Slling 			return (-1);
10185094Slling 
10195094Slling 		fval *= pow(2, shift);
10205094Slling 
10215094Slling 		if (fval > UINT64_MAX) {
10225094Slling 			if (hdl)
10235094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10245094Slling 				    "numeric value is too large"));
10255094Slling 			return (-1);
10265094Slling 		}
10275094Slling 
10285094Slling 		*num = (uint64_t)fval;
10295094Slling 	} else {
10305094Slling 		if ((shift = str2shift(hdl, end)) == -1)
10315094Slling 			return (-1);
10325094Slling 
10335094Slling 		/* Check for overflow */
10345094Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
10355094Slling 			if (hdl)
10365094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10375094Slling 				    "numeric value is too large"));
10385094Slling 			return (-1);
10395094Slling 		}
10405094Slling 
10415094Slling 		*num <<= shift;
10425094Slling 	}
10435094Slling 
10445094Slling 	return (0);
10455094Slling }
10465094Slling 
10475094Slling /*
10485094Slling  * Given a propname=value nvpair to set, parse any numeric properties
10495094Slling  * (index, boolean, etc) if they are specified as strings and add the
10505094Slling  * resulting nvpair to the returned nvlist.
10515094Slling  *
10525094Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
10535094Slling  * We want the user to be able to ignore this fact and specify properties
10545094Slling  * as native values (numbers, for example) or as strings (to simplify
10555094Slling  * command line utilities).  This also handles converting index types
10565094Slling  * (compression, checksum, etc) from strings to their on-disk index.
10575094Slling  */
10585094Slling int
10595094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
10605094Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
10615094Slling     const char *errbuf)
10624543Smarks {
10635094Slling 	data_type_t datatype = nvpair_type(elem);
10645094Slling 	zprop_type_t proptype;
10655094Slling 	const char *propname;
10665094Slling 	char *value;
10675094Slling 	boolean_t isnone = B_FALSE;
10685094Slling 
10695094Slling 	if (type == ZFS_TYPE_POOL) {
10705094Slling 		proptype = zpool_prop_get_type(prop);
10715094Slling 		propname = zpool_prop_to_name(prop);
10725094Slling 	} else {
10735094Slling 		proptype = zfs_prop_get_type(prop);
10745094Slling 		propname = zfs_prop_to_name(prop);
10755094Slling 	}
10765094Slling 
10775094Slling 	/*
10785094Slling 	 * Convert any properties to the internal DSL value types.
10795094Slling 	 */
10805094Slling 	*svalp = NULL;
10815094Slling 	*ivalp = 0;
10825094Slling 
10835094Slling 	switch (proptype) {
10845094Slling 	case PROP_TYPE_STRING:
10855094Slling 		if (datatype != DATA_TYPE_STRING) {
10865094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10875094Slling 			    "'%s' must be a string"), nvpair_name(elem));
10885094Slling 			goto error;
10895094Slling 		}
10905094Slling 		(void) nvpair_value_string(elem, svalp);
10915094Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
10925094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10935094Slling 			    "'%s' is too long"), nvpair_name(elem));
10945094Slling 			goto error;
10955094Slling 		}
10965094Slling 		break;
10975094Slling 
10985094Slling 	case PROP_TYPE_NUMBER:
10995094Slling 		if (datatype == DATA_TYPE_STRING) {
11005094Slling 			(void) nvpair_value_string(elem, &value);
11015094Slling 			if (strcmp(value, "none") == 0) {
11025094Slling 				isnone = B_TRUE;
11035094Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
11045094Slling 			    != 0) {
11055094Slling 				goto error;
11065094Slling 			}
11075094Slling 		} else if (datatype == DATA_TYPE_UINT64) {
11085094Slling 			(void) nvpair_value_uint64(elem, ivalp);
11095094Slling 		} else {
11105094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11115094Slling 			    "'%s' must be a number"), nvpair_name(elem));
11125094Slling 			goto error;
11135094Slling 		}
11145094Slling 
11155094Slling 		/*
11165094Slling 		 * Quota special: force 'none' and don't allow 0.
11175094Slling 		 */
1118*5378Sck153898 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1119*5378Sck153898 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
11205094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1121*5378Sck153898 			    "use 'none' to disable quota/refquota"));
11225094Slling 			goto error;
11235094Slling 		}
11245094Slling 		break;
11255094Slling 
11265094Slling 	case PROP_TYPE_INDEX:
1127*5378Sck153898 		if (datatype != DATA_TYPE_STRING) {
11285094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11295094Slling 			    "'%s' must be a string"), nvpair_name(elem));
11305094Slling 			goto error;
11315094Slling 		}
11325094Slling 
1133*5378Sck153898 		(void) nvpair_value_string(elem, &value);
1134*5378Sck153898 
11355094Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
11365094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11375094Slling 			    "'%s' must be one of '%s'"), propname,
11385094Slling 			    zprop_values(prop, type));
11395094Slling 			goto error;
11405094Slling 		}
11415094Slling 		break;
11425094Slling 
11435094Slling 	default:
11445094Slling 		abort();
11455094Slling 	}
11464543Smarks 
11475094Slling 	/*
11485094Slling 	 * Add the result to our return set of properties.
11495094Slling 	 */
11505094Slling 	if (*svalp != NULL) {
11515094Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
11525094Slling 			(void) no_memory(hdl);
11535094Slling 			return (-1);
11545094Slling 		}
11555094Slling 	} else {
11565094Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
11575094Slling 			(void) no_memory(hdl);
11585094Slling 			return (-1);
11595094Slling 		}
11605094Slling 	}
11615094Slling 
11625094Slling 	return (0);
11635094Slling error:
11645094Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11655094Slling 	return (-1);
11665094Slling }
11675094Slling 
11685094Slling /*
11695094Slling  * Given a comma-separated list of properties, construct a property list
11705094Slling  * containing both user-defined and native properties.  This function will
11715094Slling  * return a NULL list if 'all' is specified, which can later be expanded
11725094Slling  * by zprop_expand_list().
11735094Slling  */
11745094Slling int
11755094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
11765094Slling     zfs_type_t type)
11775094Slling {
11785094Slling 	size_t len;
11795094Slling 	char *s, *p;
11805094Slling 	char c;
11815094Slling 	int prop;
11825094Slling 	zprop_list_t *entry;
11835094Slling 	zprop_list_t **last;
11845094Slling 
11855094Slling 	*listp = NULL;
11865094Slling 	last = listp;
11875094Slling 
11885094Slling 	/*
11895094Slling 	 * If 'all' is specified, return a NULL list.
11905094Slling 	 */
11915094Slling 	if (strcmp(props, "all") == 0)
11925094Slling 		return (0);
11935094Slling 
11945094Slling 	/*
11955094Slling 	 * If no props were specified, return an error.
11965094Slling 	 */
11975094Slling 	if (props[0] == '\0') {
11985094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11995094Slling 		    "no properties specified"));
12005094Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
12015094Slling 		    "bad property list")));
12024543Smarks 	}
12035094Slling 
12045094Slling 	/*
12055094Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
12065094Slling 	 * aliases makes this more effort than it's worth.
12075094Slling 	 */
12085094Slling 	s = props;
12095094Slling 	while (*s != '\0') {
12105094Slling 		if ((p = strchr(s, ',')) == NULL) {
12115094Slling 			len = strlen(s);
12125094Slling 			p = s + len;
12135094Slling 		} else {
12145094Slling 			len = p - s;
12155094Slling 		}
12165094Slling 
12175094Slling 		/*
12185094Slling 		 * Check for empty options.
12195094Slling 		 */
12205094Slling 		if (len == 0) {
12215094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12225094Slling 			    "empty property name"));
12235094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
12245094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
12255094Slling 		}
12265094Slling 
12275094Slling 		/*
12285094Slling 		 * Check all regular property names.
12295094Slling 		 */
12305094Slling 		c = s[len];
12315094Slling 		s[len] = '\0';
12325094Slling 		prop = zprop_name_to_prop(s, type);
12335094Slling 
12345094Slling 		if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
12355094Slling 			prop = ZPROP_INVAL;
12365094Slling 
12375094Slling 		/*
12385094Slling 		 * When no property table entry can be found, return failure if
12395094Slling 		 * this is a pool property or if this isn't a user-defined
12405094Slling 		 * dataset property,
12415094Slling 		 */
12425094Slling 		if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
12435094Slling 		    !zfs_prop_user(s))) {
12445094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12455094Slling 			    "invalid property '%s'"), s);
12465094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
12475094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
12485094Slling 		}
12495094Slling 
12505094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
12515094Slling 			return (-1);
12525094Slling 
12535094Slling 		entry->pl_prop = prop;
12545094Slling 		if (prop == ZPROP_INVAL) {
12555094Slling 			if ((entry->pl_user_prop = zfs_strdup(hdl, s))
12565094Slling 			    == NULL) {
12575094Slling 				free(entry);
12585094Slling 				return (-1);
12595094Slling 			}
12605094Slling 			entry->pl_width = strlen(s);
12615094Slling 		} else {
12625094Slling 			entry->pl_width = zprop_width(prop, &entry->pl_fixed,
12635094Slling 			    type);
12645094Slling 		}
12655094Slling 
12665094Slling 		*last = entry;
12675094Slling 		last = &entry->pl_next;
12685094Slling 
12695094Slling 		s = p;
12705094Slling 		if (c == ',')
12715094Slling 			s++;
12725094Slling 	}
12735094Slling 
12745094Slling 	return (0);
12755094Slling }
12765094Slling 
12775094Slling void
12785094Slling zprop_free_list(zprop_list_t *pl)
12795094Slling {
12805094Slling 	zprop_list_t *next;
12814543Smarks 
12825094Slling 	while (pl != NULL) {
12835094Slling 		next = pl->pl_next;
12845094Slling 		free(pl->pl_user_prop);
12855094Slling 		free(pl);
12865094Slling 		pl = next;
12875094Slling 	}
12885094Slling }
12895094Slling 
12905094Slling typedef struct expand_data {
12915094Slling 	zprop_list_t	**last;
12925094Slling 	libzfs_handle_t	*hdl;
12935094Slling 	zfs_type_t type;
12945094Slling } expand_data_t;
12955094Slling 
12965094Slling int
12975094Slling zprop_expand_list_cb(int prop, void *cb)
12985094Slling {
12995094Slling 	zprop_list_t *entry;
13005094Slling 	expand_data_t *edp = cb;
13015094Slling 
13025094Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
13035094Slling 		return (ZPROP_INVAL);
13045094Slling 
13055094Slling 	entry->pl_prop = prop;
13065094Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
13075094Slling 	entry->pl_all = B_TRUE;
13085094Slling 
13095094Slling 	*(edp->last) = entry;
13105094Slling 	edp->last = &entry->pl_next;
13115094Slling 
13125094Slling 	return (ZPROP_CONT);
13134543Smarks }
13145094Slling 
13155094Slling int
13165094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
13175094Slling {
13185094Slling 	zprop_list_t *entry;
13195094Slling 	zprop_list_t **last;
13205094Slling 	expand_data_t exp;
13215094Slling 
13225094Slling 	if (*plp == NULL) {
13235094Slling 		/*
13245094Slling 		 * If this is the very first time we've been called for an 'all'
13255094Slling 		 * specification, expand the list to include all native
13265094Slling 		 * properties.
13275094Slling 		 */
13285094Slling 		last = plp;
13295094Slling 
13305094Slling 		exp.last = last;
13315094Slling 		exp.hdl = hdl;
13325094Slling 		exp.type = type;
13335094Slling 
13345094Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
13355094Slling 		    B_FALSE, type) == ZPROP_INVAL)
13365094Slling 			return (-1);
13375094Slling 
13385094Slling 		/*
13395094Slling 		 * Add 'name' to the beginning of the list, which is handled
13405094Slling 		 * specially.
13415094Slling 		 */
13425094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
13435094Slling 			return (-1);
13445094Slling 
13455094Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
13465094Slling 		    ZFS_PROP_NAME;
13475094Slling 		entry->pl_width = zprop_width(entry->pl_prop,
13485094Slling 		    &entry->pl_fixed, type);
13495094Slling 		entry->pl_all = B_TRUE;
13505094Slling 		entry->pl_next = *plp;
13515094Slling 		*plp = entry;
13525094Slling 	}
13535094Slling 	return (0);
13545094Slling }
13555094Slling 
13565094Slling int
13575094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
13585094Slling     zfs_type_t type)
13595094Slling {
13605094Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
13615094Slling }
1362