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 /*
225807Sck153898  * Copyright 2008 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"));
2045450Sbrendan 	case EZFS_ISL2CACHE:
2055450Sbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
2062082Seschrock 	case EZFS_UNKNOWN:
2072082Seschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
2082082Seschrock 	default:
2092500Seschrock 		assert(hdl->libzfs_error == 0);
2102500Seschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
2112082Seschrock 	}
2122082Seschrock }
2132082Seschrock 
2142082Seschrock /*PRINTFLIKE2*/
215789Sahrens void
2162082Seschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
217789Sahrens {
218789Sahrens 	va_list ap;
219789Sahrens 
220789Sahrens 	va_start(ap, fmt);
221789Sahrens 
2222082Seschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
2232082Seschrock 	    fmt, ap);
2242082Seschrock 	hdl->libzfs_desc_active = 1;
225789Sahrens 
226789Sahrens 	va_end(ap);
227789Sahrens }
228789Sahrens 
2292082Seschrock static void
2302082Seschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
2312082Seschrock {
2322082Seschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
2332082Seschrock 	    fmt, ap);
2342082Seschrock 	hdl->libzfs_error = error;
2352082Seschrock 
2362082Seschrock 	if (hdl->libzfs_desc_active)
2372082Seschrock 		hdl->libzfs_desc_active = 0;
2382082Seschrock 	else
2392082Seschrock 		hdl->libzfs_desc[0] = '\0';
2402082Seschrock 
2412082Seschrock 	if (hdl->libzfs_printerr) {
2422082Seschrock 		if (error == EZFS_UNKNOWN) {
2432082Seschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
2442082Seschrock 			    "error: %s\n"), libzfs_error_description(hdl));
2452082Seschrock 			abort();
2462082Seschrock 		}
2472082Seschrock 
2482082Seschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
2493912Slling 		    libzfs_error_description(hdl));
2502082Seschrock 		if (error == EZFS_NOMEM)
2512082Seschrock 			exit(1);
2522082Seschrock 	}
2532082Seschrock }
2542082Seschrock 
2553237Slling int
2563237Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
2573237Slling {
2583237Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
2593237Slling }
2603237Slling 
2612082Seschrock /*PRINTFLIKE3*/
2622082Seschrock int
2633237Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
2642082Seschrock {
2652082Seschrock 	va_list ap;
2662082Seschrock 
2672082Seschrock 	va_start(ap, fmt);
2682082Seschrock 
2692082Seschrock 	zfs_verror(hdl, error, fmt, ap);
2702082Seschrock 
2712082Seschrock 	va_end(ap);
2722082Seschrock 
2732082Seschrock 	return (-1);
2742082Seschrock }
2752082Seschrock 
2762082Seschrock static int
2772082Seschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
2782082Seschrock     va_list ap)
2792082Seschrock {
2802082Seschrock 	switch (error) {
2812082Seschrock 	case EPERM:
2822082Seschrock 	case EACCES:
2832082Seschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
2842082Seschrock 		return (-1);
2852082Seschrock 
2864543Smarks 	case ECANCELED:
2874543Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
2884543Smarks 		return (-1);
2894543Smarks 
2902082Seschrock 	case EIO:
2912082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
2922082Seschrock 		return (-1);
2932082Seschrock 
2942082Seschrock 	case EINTR:
2952082Seschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
2962082Seschrock 		return (-1);
2972082Seschrock 	}
2982082Seschrock 
2992082Seschrock 	return (0);
3002082Seschrock }
3012082Seschrock 
3023237Slling int
3033237Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3043237Slling {
3053237Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
3063237Slling }
3073237Slling 
3082082Seschrock /*PRINTFLIKE3*/
3092082Seschrock int
3103237Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
311789Sahrens {
312789Sahrens 	va_list ap;
313789Sahrens 
314789Sahrens 	va_start(ap, fmt);
315789Sahrens 
3162082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3172082Seschrock 		va_end(ap);
3182082Seschrock 		return (-1);
3192082Seschrock 	}
3202082Seschrock 
3212082Seschrock 	switch (error) {
3222082Seschrock 	case ENXIO:
3235807Sck153898 	case ENODEV:
3242082Seschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
3252082Seschrock 		break;
3262082Seschrock 
3272082Seschrock 	case ENOENT:
3282082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3292082Seschrock 		    "dataset does not exist"));
3302082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3312082Seschrock 		break;
3322082Seschrock 
3332082Seschrock 	case ENOSPC:
3342082Seschrock 	case EDQUOT:
3352082Seschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
3362082Seschrock 		return (-1);
3372082Seschrock 
3382082Seschrock 	case EEXIST:
3392082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3402082Seschrock 		    "dataset already exists"));
3412082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
3422082Seschrock 		break;
3432082Seschrock 
3442082Seschrock 	case EBUSY:
3452082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3462082Seschrock 		    "dataset is busy"));
3472082Seschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
3482082Seschrock 		break;
3494543Smarks 	case EROFS:
3504543Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3514543Smarks 		    "snapshot permissions cannot be modified"));
3524543Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
3534543Smarks 		break;
3543978Smmusante 	case ENAMETOOLONG:
3553978Smmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
3563978Smmusante 		break;
357*5860Sck153898 	case ENOTSUP:
358*5860Sck153898 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
359*5860Sck153898 		break;
3602082Seschrock 	default:
3612082Seschrock 		zfs_error_aux(hdl, strerror(errno));
3622082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
3632082Seschrock 		break;
364789Sahrens 	}
365789Sahrens 
366789Sahrens 	va_end(ap);
3672082Seschrock 	return (-1);
368789Sahrens }
369789Sahrens 
3703237Slling int
3713237Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
3723237Slling {
3733237Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
3743237Slling }
3753237Slling 
3762082Seschrock /*PRINTFLIKE3*/
3772082Seschrock int
3783237Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
379789Sahrens {
3802082Seschrock 	va_list ap;
3812082Seschrock 
3822082Seschrock 	va_start(ap, fmt);
3832082Seschrock 
3842082Seschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
3852082Seschrock 		va_end(ap);
3862082Seschrock 		return (-1);
3872082Seschrock 	}
3882082Seschrock 
3892082Seschrock 	switch (error) {
3902082Seschrock 	case ENODEV:
3912082Seschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
3922082Seschrock 		break;
3932082Seschrock 
3942082Seschrock 	case ENOENT:
3953912Slling 		zfs_error_aux(hdl,
3963912Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
3972082Seschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
3982082Seschrock 		break;
3992082Seschrock 
4002082Seschrock 	case EEXIST:
4012082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4022082Seschrock 		    "pool already exists"));
4032082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
4042082Seschrock 		break;
4052082Seschrock 
4062082Seschrock 	case EBUSY:
4072082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
4082082Seschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
4092082Seschrock 		break;
4102082Seschrock 
4112082Seschrock 	case ENXIO:
4122082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4132082Seschrock 		    "one or more devices is currently unavailable"));
4142082Seschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
4152082Seschrock 		break;
4162082Seschrock 
4172082Seschrock 	case ENAMETOOLONG:
4182082Seschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
4192082Seschrock 		break;
4202082Seschrock 
4213912Slling 	case ENOTSUP:
4223912Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
4233912Slling 		break;
4243912Slling 
4253912Slling 	case EINVAL:
4263912Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
4273912Slling 		break;
4283912Slling 
4294543Smarks 	case ENOSPC:
4304543Smarks 	case EDQUOT:
4314543Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
4324543Smarks 		return (-1);
4334543Smarks 
4342082Seschrock 	default:
4352082Seschrock 		zfs_error_aux(hdl, strerror(error));
4362082Seschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
4372082Seschrock 	}
4382082Seschrock 
4392082Seschrock 	va_end(ap);
4402082Seschrock 	return (-1);
441789Sahrens }
442789Sahrens 
443789Sahrens /*
444789Sahrens  * Display an out of memory error message and abort the current program.
445789Sahrens  */
4462082Seschrock int
4472082Seschrock no_memory(libzfs_handle_t *hdl)
448789Sahrens {
4492082Seschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
450789Sahrens }
451789Sahrens 
452789Sahrens /*
453789Sahrens  * A safe form of malloc() which will die if the allocation fails.
454789Sahrens  */
455789Sahrens void *
4562082Seschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
457789Sahrens {
458789Sahrens 	void *data;
459789Sahrens 
460789Sahrens 	if ((data = calloc(1, size)) == NULL)
4612082Seschrock 		(void) no_memory(hdl);
462789Sahrens 
463789Sahrens 	return (data);
464789Sahrens }
465789Sahrens 
466789Sahrens /*
4672676Seschrock  * A safe form of realloc(), which also zeroes newly allocated space.
4682676Seschrock  */
4692676Seschrock void *
4702676Seschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
4712676Seschrock {
4722676Seschrock 	void *ret;
4732676Seschrock 
4742676Seschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
4752676Seschrock 		(void) no_memory(hdl);
4762676Seschrock 		free(ptr);
4772676Seschrock 		return (NULL);
4782676Seschrock 	}
4792676Seschrock 
4802676Seschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
4812676Seschrock 	return (ret);
4822676Seschrock }
4832676Seschrock 
4842676Seschrock /*
485789Sahrens  * A safe form of strdup() which will die if the allocation fails.
486789Sahrens  */
487789Sahrens char *
4882082Seschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
489789Sahrens {
490789Sahrens 	char *ret;
491789Sahrens 
492789Sahrens 	if ((ret = strdup(str)) == NULL)
4932082Seschrock 		(void) no_memory(hdl);
494789Sahrens 
495789Sahrens 	return (ret);
496789Sahrens }
497789Sahrens 
498789Sahrens /*
499789Sahrens  * Convert a number to an appropriately human-readable output.
500789Sahrens  */
501789Sahrens void
502789Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
503789Sahrens {
504789Sahrens 	uint64_t n = num;
505789Sahrens 	int index = 0;
506789Sahrens 	char u;
507789Sahrens 
508789Sahrens 	while (n >= 1024) {
5091162Seschrock 		n /= 1024;
510789Sahrens 		index++;
511789Sahrens 	}
512789Sahrens 
513789Sahrens 	u = " KMGTPE"[index];
514789Sahrens 
5151162Seschrock 	if (index == 0) {
516789Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5171162Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5181162Seschrock 		/*
5191162Seschrock 		 * If this is an even multiple of the base, always display
5201162Seschrock 		 * without any decimal precision.
5211162Seschrock 		 */
522789Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5231162Seschrock 	} else {
5241162Seschrock 		/*
5251162Seschrock 		 * We want to choose a precision that reflects the best choice
5261162Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5271162Seschrock 		 * we have numbers that are very close to an order of magnitude.
5281162Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5291162Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5301162Seschrock 		 * develop some complex heuristics for this, but it's much
5311162Seschrock 		 * easier just to try each combination in turn.
5321162Seschrock 		 */
5331162Seschrock 		int i;
5341162Seschrock 		for (i = 2; i >= 0; i--) {
5354451Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5364451Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5371162Seschrock 				break;
5381162Seschrock 		}
5391162Seschrock 	}
540789Sahrens }
5412082Seschrock 
5422082Seschrock void
5432082Seschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
5442082Seschrock {
5452082Seschrock 	hdl->libzfs_printerr = printerr;
5462082Seschrock }
5472082Seschrock 
5482082Seschrock libzfs_handle_t *
5492082Seschrock libzfs_init(void)
5502082Seschrock {
5512082Seschrock 	libzfs_handle_t *hdl;
5522082Seschrock 
5532082Seschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
5542082Seschrock 		return (NULL);
5552082Seschrock 	}
5562082Seschrock 
5572500Seschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
5582082Seschrock 		free(hdl);
5592082Seschrock 		return (NULL);
5602082Seschrock 	}
5612082Seschrock 
5622082Seschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
5632082Seschrock 		(void) close(hdl->libzfs_fd);
5642082Seschrock 		free(hdl);
5652082Seschrock 		return (NULL);
5662082Seschrock 	}
5672082Seschrock 
5682082Seschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
5692082Seschrock 
5704787Sahrens 	zfs_prop_init();
5715094Slling 	zpool_prop_init();
5724787Sahrens 
5732082Seschrock 	return (hdl);
5742082Seschrock }
5752082Seschrock 
5762082Seschrock void
5772082Seschrock libzfs_fini(libzfs_handle_t *hdl)
5782082Seschrock {
5792082Seschrock 	(void) close(hdl->libzfs_fd);
5802082Seschrock 	if (hdl->libzfs_mnttab)
5812082Seschrock 		(void) fclose(hdl->libzfs_mnttab);
5822082Seschrock 	if (hdl->libzfs_sharetab)
5832082Seschrock 		(void) fclose(hdl->libzfs_sharetab);
5844180Sdougm 	zfs_uninit_libshare(hdl);
5854543Smarks 	if (hdl->libzfs_log_str)
5864543Smarks 		(void) free(hdl->libzfs_log_str);
5872082Seschrock 	namespace_clear(hdl);
5882082Seschrock 	free(hdl);
5892082Seschrock }
5902082Seschrock 
5912082Seschrock libzfs_handle_t *
5922082Seschrock zpool_get_handle(zpool_handle_t *zhp)
5932082Seschrock {
5942082Seschrock 	return (zhp->zpool_hdl);
5952082Seschrock }
5962082Seschrock 
5972082Seschrock libzfs_handle_t *
5982082Seschrock zfs_get_handle(zfs_handle_t *zhp)
5992082Seschrock {
6002082Seschrock 	return (zhp->zfs_hdl);
6012082Seschrock }
6022676Seschrock 
6032676Seschrock /*
6043635Sck153898  * Given a name, determine whether or not it's a valid path
6053635Sck153898  * (starts with '/' or "./").  If so, walk the mnttab trying
6063635Sck153898  * to match the device number.  If not, treat the path as an
6073635Sck153898  * fs/vol/snap name.
6083635Sck153898  */
6093635Sck153898 zfs_handle_t *
6103635Sck153898 zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6113635Sck153898 {
6123635Sck153898 	struct stat64 statbuf;
6133635Sck153898 	struct extmnttab entry;
6143635Sck153898 	int ret;
6153635Sck153898 
6163635Sck153898 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6173635Sck153898 		/*
6183635Sck153898 		 * It's not a valid path, assume it's a name of type 'argtype'.
6193635Sck153898 		 */
6203635Sck153898 		return (zfs_open(hdl, path, argtype));
6213635Sck153898 	}
6223635Sck153898 
6233635Sck153898 	if (stat64(path, &statbuf) != 0) {
6243635Sck153898 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6253635Sck153898 		return (NULL);
6263635Sck153898 	}
6273635Sck153898 
6283635Sck153898 	rewind(hdl->libzfs_mnttab);
6293635Sck153898 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6303635Sck153898 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6313635Sck153898 		    statbuf.st_dev) {
6323635Sck153898 			break;
6333635Sck153898 		}
6343635Sck153898 	}
6353635Sck153898 	if (ret != 0) {
6363635Sck153898 		return (NULL);
6373635Sck153898 	}
6383635Sck153898 
6393635Sck153898 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6403635Sck153898 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6413635Sck153898 		    path);
6423635Sck153898 		return (NULL);
6433635Sck153898 	}
6443635Sck153898 
6453635Sck153898 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6463635Sck153898 }
6473635Sck153898 
6483635Sck153898 /*
6492676Seschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
6502676Seschrock  * an ioctl().
6512676Seschrock  */
6522676Seschrock int
6532676Seschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
6542676Seschrock {
6552676Seschrock 	if (len == 0)
6562885Sahrens 		len = 2048;
6572676Seschrock 	zc->zc_nvlist_dst_size = len;
6582676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
6592676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
6602676Seschrock 		return (-1);
6612676Seschrock 
6622676Seschrock 	return (0);
6632676Seschrock }
6642676Seschrock 
6652676Seschrock /*
6662676Seschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
6672676Seschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
6682676Seschrock  * filled in by the kernel to indicate the actual required size.
6692676Seschrock  */
6702676Seschrock int
6712676Seschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
6722676Seschrock {
6732676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
6742676Seschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
6752676Seschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
6762676Seschrock 	    == NULL)
6772676Seschrock 		return (-1);
6782676Seschrock 
6792676Seschrock 	return (0);
6802676Seschrock }
6812676Seschrock 
6822676Seschrock /*
6832885Sahrens  * Called to free the src and dst nvlists stored in the command structure.
6842676Seschrock  */
6852676Seschrock void
6862676Seschrock zcmd_free_nvlists(zfs_cmd_t *zc)
6872676Seschrock {
6885094Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
6892676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
6902676Seschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
6912676Seschrock }
6922676Seschrock 
6935094Slling static int
6945094Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
6955094Slling     nvlist_t *nvl)
6962676Seschrock {
6972676Seschrock 	char *packed;
6982676Seschrock 	size_t len;
6992676Seschrock 
7002676Seschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
7012676Seschrock 
7022676Seschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
7032676Seschrock 		return (-1);
7042676Seschrock 
7052676Seschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
7062676Seschrock 
7075094Slling 	*outnv = (uint64_t)(uintptr_t)packed;
7085094Slling 	*outlen = len;
7095094Slling 
7105094Slling 	return (0);
7115094Slling }
7122676Seschrock 
7135094Slling int
7145094Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7155094Slling {
7165094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
7175094Slling 	    &zc->zc_nvlist_conf_size, nvl));
7185094Slling }
7195094Slling 
7205094Slling int
7215094Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
7225094Slling {
7235094Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
7245094Slling 	    &zc->zc_nvlist_src_size, nvl));
7252676Seschrock }
7262676Seschrock 
7272676Seschrock /*
7282676Seschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
7292676Seschrock  */
7302676Seschrock int
7312676Seschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
7322676Seschrock {
7332676Seschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
7342676Seschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
7352676Seschrock 		return (no_memory(hdl));
7362676Seschrock 
7372676Seschrock 	return (0);
7382676Seschrock }
7393912Slling 
7405094Slling int
7415094Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
7425094Slling {
7435094Slling 	int error;
7445094Slling 
7455094Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
7465094Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
7475094Slling 	if (hdl->libzfs_log_str) {
7485094Slling 		free(hdl->libzfs_log_str);
7495094Slling 		hdl->libzfs_log_str = NULL;
7505094Slling 	}
7515094Slling 	zc->zc_history = 0;
7525094Slling 
7535094Slling 	return (error);
7545094Slling }
7555094Slling 
7565094Slling /*
7575094Slling  * ================================================================
7585094Slling  * API shared by zfs and zpool property management
7595094Slling  * ================================================================
7605094Slling  */
7615094Slling 
7623912Slling static void
7635094Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
7643912Slling {
7655094Slling 	zprop_list_t *pl = cbp->cb_proplist;
7663912Slling 	int i;
7673912Slling 	char *title;
7683912Slling 	size_t len;
7693912Slling 
7703912Slling 	cbp->cb_first = B_FALSE;
7713912Slling 	if (cbp->cb_scripted)
7723912Slling 		return;
7733912Slling 
7743912Slling 	/*
7753912Slling 	 * Start with the length of the column headers.
7763912Slling 	 */
7773912Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
7783912Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
7793912Slling 	    "PROPERTY"));
7803912Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
7813912Slling 	    "VALUE"));
7823912Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
7833912Slling 	    "SOURCE"));
7843912Slling 
7853912Slling 	/*
7863912Slling 	 * Go through and calculate the widths for each column.  For the
7873912Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
7883912Slling 	 * inheriting from the longest name.  This is acceptable because in the
7893912Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
7903912Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
7913912Slling 	 * if the name of the property is much longer the any values we find.
7923912Slling 	 */
7933912Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
7943912Slling 		/*
7953912Slling 		 * 'PROPERTY' column
7963912Slling 		 */
7975094Slling 		if (pl->pl_prop != ZPROP_INVAL) {
7985094Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
7995094Slling 			    zpool_prop_to_name(pl->pl_prop) :
8005094Slling 			    zfs_prop_to_name(pl->pl_prop);
8015094Slling 
8025094Slling 			len = strlen(propname);
8033912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8043912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8053912Slling 		} else {
8063912Slling 			len = strlen(pl->pl_user_prop);
8073912Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
8083912Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
8093912Slling 		}
8103912Slling 
8113912Slling 		/*
8123912Slling 		 * 'VALUE' column
8133912Slling 		 */
8143912Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
8153912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
8163912Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
8173912Slling 
8183912Slling 		/*
8193912Slling 		 * 'NAME' and 'SOURCE' columns
8203912Slling 		 */
8215094Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
8225094Slling 		    ZFS_PROP_NAME) &&
8233912Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
8243912Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
8253912Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
8263912Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
8273912Slling 		}
8283912Slling 	}
8293912Slling 
8303912Slling 	/*
8313912Slling 	 * Now go through and print the headers.
8323912Slling 	 */
8333912Slling 	for (i = 0; i < 4; i++) {
8343912Slling 		switch (cbp->cb_columns[i]) {
8353912Slling 		case GET_COL_NAME:
8363912Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
8373912Slling 			break;
8383912Slling 		case GET_COL_PROPERTY:
8393912Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
8403912Slling 			break;
8413912Slling 		case GET_COL_VALUE:
8423912Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
8433912Slling 			break;
8443912Slling 		case GET_COL_SOURCE:
8453912Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
8463912Slling 			break;
8473912Slling 		default:
8483912Slling 			title = NULL;
8493912Slling 		}
8503912Slling 
8513912Slling 		if (title != NULL) {
8523912Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
8533912Slling 				(void) printf("%s", title);
8543912Slling 			else
8553912Slling 				(void) printf("%-*s  ",
8563912Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
8573912Slling 				    title);
8583912Slling 		}
8593912Slling 	}
8603912Slling 	(void) printf("\n");
8613912Slling }
8623912Slling 
8633912Slling /*
8643912Slling  * Display a single line of output, according to the settings in the callback
8653912Slling  * structure.
8663912Slling  */
8673912Slling void
8685094Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
8695094Slling     const char *propname, const char *value, zprop_source_t sourcetype,
8703912Slling     const char *source)
8713912Slling {
8723912Slling 	int i;
8733912Slling 	const char *str;
8743912Slling 	char buf[128];
8753912Slling 
8763912Slling 	/*
8773912Slling 	 * Ignore those source types that the user has chosen to ignore.
8783912Slling 	 */
8793912Slling 	if ((sourcetype & cbp->cb_sources) == 0)
8803912Slling 		return;
8813912Slling 
8823912Slling 	if (cbp->cb_first)
8835094Slling 		zprop_print_headers(cbp, cbp->cb_type);
8843912Slling 
8853912Slling 	for (i = 0; i < 4; i++) {
8863912Slling 		switch (cbp->cb_columns[i]) {
8873912Slling 		case GET_COL_NAME:
8883912Slling 			str = name;
8893912Slling 			break;
8903912Slling 
8913912Slling 		case GET_COL_PROPERTY:
8923912Slling 			str = propname;
8933912Slling 			break;
8943912Slling 
8953912Slling 		case GET_COL_VALUE:
8963912Slling 			str = value;
8973912Slling 			break;
8983912Slling 
8993912Slling 		case GET_COL_SOURCE:
9003912Slling 			switch (sourcetype) {
9015094Slling 			case ZPROP_SRC_NONE:
9023912Slling 				str = "-";
9033912Slling 				break;
9043912Slling 
9055094Slling 			case ZPROP_SRC_DEFAULT:
9063912Slling 				str = "default";
9073912Slling 				break;
9083912Slling 
9095094Slling 			case ZPROP_SRC_LOCAL:
9103912Slling 				str = "local";
9113912Slling 				break;
9123912Slling 
9135094Slling 			case ZPROP_SRC_TEMPORARY:
9143912Slling 				str = "temporary";
9153912Slling 				break;
9163912Slling 
9175094Slling 			case ZPROP_SRC_INHERITED:
9183912Slling 				(void) snprintf(buf, sizeof (buf),
9193912Slling 				    "inherited from %s", source);
9203912Slling 				str = buf;
9213912Slling 				break;
9223912Slling 			}
9233912Slling 			break;
9243912Slling 
9253912Slling 		default:
9263912Slling 			continue;
9273912Slling 		}
9283912Slling 
9293912Slling 		if (cbp->cb_columns[i + 1] == 0)
9303912Slling 			(void) printf("%s", str);
9313912Slling 		else if (cbp->cb_scripted)
9323912Slling 			(void) printf("%s\t", str);
9333912Slling 		else
9343912Slling 			(void) printf("%-*s  ",
9353912Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
9363912Slling 			    str);
9373912Slling 
9383912Slling 	}
9393912Slling 
9403912Slling 	(void) printf("\n");
9413912Slling }
9424543Smarks 
9435094Slling /*
9445094Slling  * Given a numeric suffix, convert the value into a number of bits that the
9455094Slling  * resulting value must be shifted.
9465094Slling  */
9475094Slling static int
9485094Slling str2shift(libzfs_handle_t *hdl, const char *buf)
9495094Slling {
9505094Slling 	const char *ends = "BKMGTPEZ";
9515094Slling 	int i;
9525094Slling 
9535094Slling 	if (buf[0] == '\0')
9545094Slling 		return (0);
9555094Slling 	for (i = 0; i < strlen(ends); i++) {
9565094Slling 		if (toupper(buf[0]) == ends[i])
9575094Slling 			break;
9585094Slling 	}
9595094Slling 	if (i == strlen(ends)) {
9605094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9615094Slling 		    "invalid numeric suffix '%s'"), buf);
9625094Slling 		return (-1);
9635094Slling 	}
9645094Slling 
9655094Slling 	/*
9665094Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
9675094Slling 	 * allow 'BB' - that's just weird.
9685094Slling 	 */
9695094Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
9705094Slling 	    toupper(buf[0]) != 'B'))
9715094Slling 		return (10*i);
9725094Slling 
9735094Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9745094Slling 	    "invalid numeric suffix '%s'"), buf);
9755094Slling 	return (-1);
9765094Slling }
9775094Slling 
9785094Slling /*
9795094Slling  * Convert a string of the form '100G' into a real number.  Used when setting
9805094Slling  * properties or creating a volume.  'buf' is used to place an extended error
9815094Slling  * message for the caller to use.
9825094Slling  */
9834543Smarks int
9845094Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
9855094Slling {
9865094Slling 	char *end;
9875094Slling 	int shift;
9885094Slling 
9895094Slling 	*num = 0;
9905094Slling 
9915094Slling 	/* Check to see if this looks like a number.  */
9925094Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
9935094Slling 		if (hdl)
9945094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9955094Slling 			    "bad numeric value '%s'"), value);
9965094Slling 		return (-1);
9975094Slling 	}
9985094Slling 
9995094Slling 	/* Rely on stroll() to process the numeric portion.  */
10005094Slling 	errno = 0;
10015094Slling 	*num = strtoll(value, &end, 10);
10025094Slling 
10035094Slling 	/*
10045094Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
10055094Slling 	 * in a 64-bit value.
10065094Slling 	 */
10075094Slling 	if (errno == ERANGE) {
10085094Slling 		if (hdl)
10095094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10105094Slling 			    "numeric value is too large"));
10115094Slling 		return (-1);
10125094Slling 	}
10135094Slling 
10145094Slling 	/*
10155094Slling 	 * If we have a decimal value, then do the computation with floating
10165094Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
10175094Slling 	 */
10185094Slling 	if (*end == '.') {
10195094Slling 		double fval = strtod(value, &end);
10205094Slling 
10215094Slling 		if ((shift = str2shift(hdl, end)) == -1)
10225094Slling 			return (-1);
10235094Slling 
10245094Slling 		fval *= pow(2, shift);
10255094Slling 
10265094Slling 		if (fval > UINT64_MAX) {
10275094Slling 			if (hdl)
10285094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10295094Slling 				    "numeric value is too large"));
10305094Slling 			return (-1);
10315094Slling 		}
10325094Slling 
10335094Slling 		*num = (uint64_t)fval;
10345094Slling 	} else {
10355094Slling 		if ((shift = str2shift(hdl, end)) == -1)
10365094Slling 			return (-1);
10375094Slling 
10385094Slling 		/* Check for overflow */
10395094Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
10405094Slling 			if (hdl)
10415094Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10425094Slling 				    "numeric value is too large"));
10435094Slling 			return (-1);
10445094Slling 		}
10455094Slling 
10465094Slling 		*num <<= shift;
10475094Slling 	}
10485094Slling 
10495094Slling 	return (0);
10505094Slling }
10515094Slling 
10525094Slling /*
10535094Slling  * Given a propname=value nvpair to set, parse any numeric properties
10545094Slling  * (index, boolean, etc) if they are specified as strings and add the
10555094Slling  * resulting nvpair to the returned nvlist.
10565094Slling  *
10575094Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
10585094Slling  * We want the user to be able to ignore this fact and specify properties
10595094Slling  * as native values (numbers, for example) or as strings (to simplify
10605094Slling  * command line utilities).  This also handles converting index types
10615094Slling  * (compression, checksum, etc) from strings to their on-disk index.
10625094Slling  */
10635094Slling int
10645094Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
10655094Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
10665094Slling     const char *errbuf)
10674543Smarks {
10685094Slling 	data_type_t datatype = nvpair_type(elem);
10695094Slling 	zprop_type_t proptype;
10705094Slling 	const char *propname;
10715094Slling 	char *value;
10725094Slling 	boolean_t isnone = B_FALSE;
10735094Slling 
10745094Slling 	if (type == ZFS_TYPE_POOL) {
10755094Slling 		proptype = zpool_prop_get_type(prop);
10765094Slling 		propname = zpool_prop_to_name(prop);
10775094Slling 	} else {
10785094Slling 		proptype = zfs_prop_get_type(prop);
10795094Slling 		propname = zfs_prop_to_name(prop);
10805094Slling 	}
10815094Slling 
10825094Slling 	/*
10835094Slling 	 * Convert any properties to the internal DSL value types.
10845094Slling 	 */
10855094Slling 	*svalp = NULL;
10865094Slling 	*ivalp = 0;
10875094Slling 
10885094Slling 	switch (proptype) {
10895094Slling 	case PROP_TYPE_STRING:
10905094Slling 		if (datatype != DATA_TYPE_STRING) {
10915094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10925094Slling 			    "'%s' must be a string"), nvpair_name(elem));
10935094Slling 			goto error;
10945094Slling 		}
10955094Slling 		(void) nvpair_value_string(elem, svalp);
10965094Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
10975094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10985094Slling 			    "'%s' is too long"), nvpair_name(elem));
10995094Slling 			goto error;
11005094Slling 		}
11015094Slling 		break;
11025094Slling 
11035094Slling 	case PROP_TYPE_NUMBER:
11045094Slling 		if (datatype == DATA_TYPE_STRING) {
11055094Slling 			(void) nvpair_value_string(elem, &value);
11065094Slling 			if (strcmp(value, "none") == 0) {
11075094Slling 				isnone = B_TRUE;
11085094Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
11095094Slling 			    != 0) {
11105094Slling 				goto error;
11115094Slling 			}
11125094Slling 		} else if (datatype == DATA_TYPE_UINT64) {
11135094Slling 			(void) nvpair_value_uint64(elem, ivalp);
11145094Slling 		} else {
11155094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11165094Slling 			    "'%s' must be a number"), nvpair_name(elem));
11175094Slling 			goto error;
11185094Slling 		}
11195094Slling 
11205094Slling 		/*
11215094Slling 		 * Quota special: force 'none' and don't allow 0.
11225094Slling 		 */
11235378Sck153898 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
11245378Sck153898 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
11255094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11265378Sck153898 			    "use 'none' to disable quota/refquota"));
11275094Slling 			goto error;
11285094Slling 		}
11295094Slling 		break;
11305094Slling 
11315094Slling 	case PROP_TYPE_INDEX:
11325378Sck153898 		if (datatype != DATA_TYPE_STRING) {
11335094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11345094Slling 			    "'%s' must be a string"), nvpair_name(elem));
11355094Slling 			goto error;
11365094Slling 		}
11375094Slling 
11385378Sck153898 		(void) nvpair_value_string(elem, &value);
11395378Sck153898 
11405094Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
11415094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11425094Slling 			    "'%s' must be one of '%s'"), propname,
11435094Slling 			    zprop_values(prop, type));
11445094Slling 			goto error;
11455094Slling 		}
11465094Slling 		break;
11475094Slling 
11485094Slling 	default:
11495094Slling 		abort();
11505094Slling 	}
11514543Smarks 
11525094Slling 	/*
11535094Slling 	 * Add the result to our return set of properties.
11545094Slling 	 */
11555094Slling 	if (*svalp != NULL) {
11565094Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
11575094Slling 			(void) no_memory(hdl);
11585094Slling 			return (-1);
11595094Slling 		}
11605094Slling 	} else {
11615094Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
11625094Slling 			(void) no_memory(hdl);
11635094Slling 			return (-1);
11645094Slling 		}
11655094Slling 	}
11665094Slling 
11675094Slling 	return (0);
11685094Slling error:
11695094Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
11705094Slling 	return (-1);
11715094Slling }
11725094Slling 
11735094Slling /*
11745094Slling  * Given a comma-separated list of properties, construct a property list
11755094Slling  * containing both user-defined and native properties.  This function will
11765094Slling  * return a NULL list if 'all' is specified, which can later be expanded
11775094Slling  * by zprop_expand_list().
11785094Slling  */
11795094Slling int
11805094Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
11815094Slling     zfs_type_t type)
11825094Slling {
11835094Slling 	size_t len;
11845094Slling 	char *s, *p;
11855094Slling 	char c;
11865094Slling 	int prop;
11875094Slling 	zprop_list_t *entry;
11885094Slling 	zprop_list_t **last;
11895094Slling 
11905094Slling 	*listp = NULL;
11915094Slling 	last = listp;
11925094Slling 
11935094Slling 	/*
11945094Slling 	 * If 'all' is specified, return a NULL list.
11955094Slling 	 */
11965094Slling 	if (strcmp(props, "all") == 0)
11975094Slling 		return (0);
11985094Slling 
11995094Slling 	/*
12005094Slling 	 * If no props were specified, return an error.
12015094Slling 	 */
12025094Slling 	if (props[0] == '\0') {
12035094Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12045094Slling 		    "no properties specified"));
12055094Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
12065094Slling 		    "bad property list")));
12074543Smarks 	}
12085094Slling 
12095094Slling 	/*
12105094Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
12115094Slling 	 * aliases makes this more effort than it's worth.
12125094Slling 	 */
12135094Slling 	s = props;
12145094Slling 	while (*s != '\0') {
12155094Slling 		if ((p = strchr(s, ',')) == NULL) {
12165094Slling 			len = strlen(s);
12175094Slling 			p = s + len;
12185094Slling 		} else {
12195094Slling 			len = p - s;
12205094Slling 		}
12215094Slling 
12225094Slling 		/*
12235094Slling 		 * Check for empty options.
12245094Slling 		 */
12255094Slling 		if (len == 0) {
12265094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12275094Slling 			    "empty property name"));
12285094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
12295094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
12305094Slling 		}
12315094Slling 
12325094Slling 		/*
12335094Slling 		 * Check all regular property names.
12345094Slling 		 */
12355094Slling 		c = s[len];
12365094Slling 		s[len] = '\0';
12375094Slling 		prop = zprop_name_to_prop(s, type);
12385094Slling 
12395094Slling 		if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
12405094Slling 			prop = ZPROP_INVAL;
12415094Slling 
12425094Slling 		/*
12435094Slling 		 * When no property table entry can be found, return failure if
12445094Slling 		 * this is a pool property or if this isn't a user-defined
12455094Slling 		 * dataset property,
12465094Slling 		 */
12475094Slling 		if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
12485094Slling 		    !zfs_prop_user(s))) {
12495094Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12505094Slling 			    "invalid property '%s'"), s);
12515094Slling 			return (zfs_error(hdl, EZFS_BADPROP,
12525094Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
12535094Slling 		}
12545094Slling 
12555094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
12565094Slling 			return (-1);
12575094Slling 
12585094Slling 		entry->pl_prop = prop;
12595094Slling 		if (prop == ZPROP_INVAL) {
12605094Slling 			if ((entry->pl_user_prop = zfs_strdup(hdl, s))
12615094Slling 			    == NULL) {
12625094Slling 				free(entry);
12635094Slling 				return (-1);
12645094Slling 			}
12655094Slling 			entry->pl_width = strlen(s);
12665094Slling 		} else {
12675094Slling 			entry->pl_width = zprop_width(prop, &entry->pl_fixed,
12685094Slling 			    type);
12695094Slling 		}
12705094Slling 
12715094Slling 		*last = entry;
12725094Slling 		last = &entry->pl_next;
12735094Slling 
12745094Slling 		s = p;
12755094Slling 		if (c == ',')
12765094Slling 			s++;
12775094Slling 	}
12785094Slling 
12795094Slling 	return (0);
12805094Slling }
12815094Slling 
12825094Slling void
12835094Slling zprop_free_list(zprop_list_t *pl)
12845094Slling {
12855094Slling 	zprop_list_t *next;
12864543Smarks 
12875094Slling 	while (pl != NULL) {
12885094Slling 		next = pl->pl_next;
12895094Slling 		free(pl->pl_user_prop);
12905094Slling 		free(pl);
12915094Slling 		pl = next;
12925094Slling 	}
12935094Slling }
12945094Slling 
12955094Slling typedef struct expand_data {
12965094Slling 	zprop_list_t	**last;
12975094Slling 	libzfs_handle_t	*hdl;
12985094Slling 	zfs_type_t type;
12995094Slling } expand_data_t;
13005094Slling 
13015094Slling int
13025094Slling zprop_expand_list_cb(int prop, void *cb)
13035094Slling {
13045094Slling 	zprop_list_t *entry;
13055094Slling 	expand_data_t *edp = cb;
13065094Slling 
13075094Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
13085094Slling 		return (ZPROP_INVAL);
13095094Slling 
13105094Slling 	entry->pl_prop = prop;
13115094Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
13125094Slling 	entry->pl_all = B_TRUE;
13135094Slling 
13145094Slling 	*(edp->last) = entry;
13155094Slling 	edp->last = &entry->pl_next;
13165094Slling 
13175094Slling 	return (ZPROP_CONT);
13184543Smarks }
13195094Slling 
13205094Slling int
13215094Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
13225094Slling {
13235094Slling 	zprop_list_t *entry;
13245094Slling 	zprop_list_t **last;
13255094Slling 	expand_data_t exp;
13265094Slling 
13275094Slling 	if (*plp == NULL) {
13285094Slling 		/*
13295094Slling 		 * If this is the very first time we've been called for an 'all'
13305094Slling 		 * specification, expand the list to include all native
13315094Slling 		 * properties.
13325094Slling 		 */
13335094Slling 		last = plp;
13345094Slling 
13355094Slling 		exp.last = last;
13365094Slling 		exp.hdl = hdl;
13375094Slling 		exp.type = type;
13385094Slling 
13395094Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
13405094Slling 		    B_FALSE, type) == ZPROP_INVAL)
13415094Slling 			return (-1);
13425094Slling 
13435094Slling 		/*
13445094Slling 		 * Add 'name' to the beginning of the list, which is handled
13455094Slling 		 * specially.
13465094Slling 		 */
13475094Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
13485094Slling 			return (-1);
13495094Slling 
13505094Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
13515094Slling 		    ZFS_PROP_NAME;
13525094Slling 		entry->pl_width = zprop_width(entry->pl_prop,
13535094Slling 		    &entry->pl_fixed, type);
13545094Slling 		entry->pl_all = B_TRUE;
13555094Slling 		entry->pl_next = *plp;
13565094Slling 		*plp = entry;
13575094Slling 	}
13585094Slling 	return (0);
13595094Slling }
13605094Slling 
13615094Slling int
13625094Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
13635094Slling     zfs_type_t type)
13645094Slling {
13655094Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
13665094Slling }
1367