xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision 990b4856d0eaada6f8140335733a1b1771ed2746)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
225aba80dbSck  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Internal utility routines for the ZFS library.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <fcntl.h>
34fa9e4066Sahrens #include <libintl.h>
35fa9e4066Sahrens #include <stdarg.h>
36fa9e4066Sahrens #include <stdio.h>
37fa9e4066Sahrens #include <stdlib.h>
38fa9e4066Sahrens #include <strings.h>
39fa9e4066Sahrens #include <unistd.h>
40*990b4856Slling #include <ctype.h>
41*990b4856Slling #include <math.h>
42fa9e4066Sahrens #include <sys/mnttab.h>
435aba80dbSck #include <sys/mntent.h>
445aba80dbSck #include <sys/types.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens #include <libzfs.h>
47fa9e4066Sahrens 
48fa9e4066Sahrens #include "libzfs_impl.h"
4991ebeef5Sahrens #include "zfs_prop.h"
50fa9e4066Sahrens 
5199653d4eSeschrock int
5299653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
5399653d4eSeschrock {
5499653d4eSeschrock 	return (hdl->libzfs_error);
5599653d4eSeschrock }
56fa9e4066Sahrens 
5799653d4eSeschrock const char *
5899653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
5999653d4eSeschrock {
6099653d4eSeschrock 	return (hdl->libzfs_action);
6199653d4eSeschrock }
62fa9e4066Sahrens 
6399653d4eSeschrock const char *
6499653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl)
6599653d4eSeschrock {
6699653d4eSeschrock 	if (hdl->libzfs_desc[0] != '\0')
6799653d4eSeschrock 		return (hdl->libzfs_desc);
6899653d4eSeschrock 
6999653d4eSeschrock 	switch (hdl->libzfs_error) {
7099653d4eSeschrock 	case EZFS_NOMEM:
7199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
7299653d4eSeschrock 	case EZFS_BADPROP:
7399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
7499653d4eSeschrock 	case EZFS_PROPREADONLY:
7599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "read only property"));
7699653d4eSeschrock 	case EZFS_PROPTYPE:
7799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
7899653d4eSeschrock 		    "datasets of this type"));
7999653d4eSeschrock 	case EZFS_PROPNONINHERIT:
8099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
8199653d4eSeschrock 	case EZFS_PROPSPACE:
8299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
8399653d4eSeschrock 	case EZFS_BADTYPE:
8499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
8599653d4eSeschrock 		    "datasets of this type"));
8699653d4eSeschrock 	case EZFS_BUSY:
8799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
8899653d4eSeschrock 	case EZFS_EXISTS:
8999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
9099653d4eSeschrock 	case EZFS_NOENT:
9199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
9299653d4eSeschrock 	case EZFS_BADSTREAM:
9399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
9499653d4eSeschrock 	case EZFS_DSREADONLY:
9599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
9699653d4eSeschrock 	case EZFS_VOLTOOBIG:
9799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
9899653d4eSeschrock 		    "this system"));
9999653d4eSeschrock 	case EZFS_VOLHASDATA:
10099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume has data"));
10199653d4eSeschrock 	case EZFS_INVALIDNAME:
10299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
10399653d4eSeschrock 	case EZFS_BADRESTORE:
10499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
10599653d4eSeschrock 		    "destination"));
10699653d4eSeschrock 	case EZFS_BADBACKUP:
10799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
10899653d4eSeschrock 	case EZFS_BADTARGET:
10999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
11099653d4eSeschrock 	case EZFS_NODEVICE:
11199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
11299653d4eSeschrock 	case EZFS_BADDEV:
11399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
11499653d4eSeschrock 	case EZFS_NOREPLICAS:
11599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
11699653d4eSeschrock 	case EZFS_RESILVERING:
11799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
11899653d4eSeschrock 	case EZFS_BADVERSION:
11999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
12099653d4eSeschrock 	case EZFS_POOLUNAVAIL:
12199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
12299653d4eSeschrock 	case EZFS_DEVOVERFLOW:
12399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
12499653d4eSeschrock 	case EZFS_BADPATH:
12599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
12699653d4eSeschrock 	case EZFS_CROSSTARGET:
12799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
12899653d4eSeschrock 		    "pools"));
12999653d4eSeschrock 	case EZFS_ZONED:
13099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
13199653d4eSeschrock 	case EZFS_MOUNTFAILED:
13299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
13399653d4eSeschrock 	case EZFS_UMOUNTFAILED:
13499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
135f3861e1aSahl 	case EZFS_UNSHARENFSFAILED:
13699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
137f3861e1aSahl 	case EZFS_SHARENFSFAILED:
13899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
139ecd6cf80Smarks 	case EZFS_ISCSISVCUNAVAIL:
140ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN,
141ecd6cf80Smarks 		    "iscsitgt service need to be enabled by "
142ecd6cf80Smarks 		    "a privileged user"));
14399653d4eSeschrock 	case EZFS_DEVLINKS:
14499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
14599653d4eSeschrock 	case EZFS_PERM:
14699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
14799653d4eSeschrock 	case EZFS_NOSPC:
14899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
14999653d4eSeschrock 	case EZFS_IO:
15099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
15199653d4eSeschrock 	case EZFS_INTR:
15299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
15399653d4eSeschrock 	case EZFS_ISSPARE:
15499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
15599653d4eSeschrock 		    "spare"));
15699653d4eSeschrock 	case EZFS_INVALCONFIG:
15799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1583bb79becSeschrock 	case EZFS_RECURSIVE:
1593bb79becSeschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
16006eeb2adSek 	case EZFS_NOHISTORY:
16106eeb2adSek 		return (dgettext(TEXT_DOMAIN, "no history available"));
162f3861e1aSahl 	case EZFS_UNSHAREISCSIFAILED:
163f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
164f3861e1aSahl 		    "iscsitgtd failed request to unshare"));
165f3861e1aSahl 	case EZFS_SHAREISCSIFAILED:
166f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
167f3861e1aSahl 		    "iscsitgtd failed request to share"));
168b1b8ab34Slling 	case EZFS_POOLPROPS:
169b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
170b1b8ab34Slling 		    "pool properties"));
171b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
172b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
173b1b8ab34Slling 		    "on this type of pool"));
174b1b8ab34Slling 	case EZFS_POOL_INVALARG:
175b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
176b1b8ab34Slling 		    "this pool operation"));
177b7661cccSmmusante 	case EZFS_NAMETOOLONG:
178b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1798488aeb5Staylor 	case EZFS_OPENFAILED:
1808488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1818488aeb5Staylor 	case EZFS_NOCAP:
1828488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1838488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1848488aeb5Staylor 	case EZFS_LABELFAILED:
1858488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
186ecd6cf80Smarks 	case EZFS_BADWHO:
187ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
188ecd6cf80Smarks 	case EZFS_BADPERM:
189ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
190ecd6cf80Smarks 	case EZFS_BADPERMSET:
191ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
192ecd6cf80Smarks 	case EZFS_NODELEGATION:
193ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
194ecd6cf80Smarks 		    "disabled on pool"));
195ecd6cf80Smarks 	case EZFS_PERMRDONLY:
196ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
197ecd6cf80Smarks 		    " modified"));
19899653d4eSeschrock 	case EZFS_UNKNOWN:
19999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
20099653d4eSeschrock 	default:
201c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
202c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
20399653d4eSeschrock 	}
20499653d4eSeschrock }
20599653d4eSeschrock 
20699653d4eSeschrock /*PRINTFLIKE2*/
207fa9e4066Sahrens void
20899653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
209fa9e4066Sahrens {
210fa9e4066Sahrens 	va_list ap;
211fa9e4066Sahrens 
212fa9e4066Sahrens 	va_start(ap, fmt);
213fa9e4066Sahrens 
21499653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
21599653d4eSeschrock 	    fmt, ap);
21699653d4eSeschrock 	hdl->libzfs_desc_active = 1;
21799653d4eSeschrock 
21899653d4eSeschrock 	va_end(ap);
21999653d4eSeschrock }
22099653d4eSeschrock 
22199653d4eSeschrock static void
22299653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
22399653d4eSeschrock {
22499653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
22599653d4eSeschrock 	    fmt, ap);
22699653d4eSeschrock 	hdl->libzfs_error = error;
22799653d4eSeschrock 
22899653d4eSeschrock 	if (hdl->libzfs_desc_active)
22999653d4eSeschrock 		hdl->libzfs_desc_active = 0;
23099653d4eSeschrock 	else
23199653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
23299653d4eSeschrock 
23399653d4eSeschrock 	if (hdl->libzfs_printerr) {
23499653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
23599653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
23699653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
23799653d4eSeschrock 			abort();
23899653d4eSeschrock 		}
23999653d4eSeschrock 
24099653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
241b1b8ab34Slling 		    libzfs_error_description(hdl));
24299653d4eSeschrock 		if (error == EZFS_NOMEM)
24399653d4eSeschrock 			exit(1);
244fa9e4066Sahrens 	}
24599653d4eSeschrock }
24699653d4eSeschrock 
247ece3d9b3Slling int
248ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
249ece3d9b3Slling {
250ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
251ece3d9b3Slling }
252ece3d9b3Slling 
25399653d4eSeschrock /*PRINTFLIKE3*/
25499653d4eSeschrock int
255ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
25699653d4eSeschrock {
25799653d4eSeschrock 	va_list ap;
25899653d4eSeschrock 
25999653d4eSeschrock 	va_start(ap, fmt);
26099653d4eSeschrock 
26199653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
262fa9e4066Sahrens 
263fa9e4066Sahrens 	va_end(ap);
26499653d4eSeschrock 
26599653d4eSeschrock 	return (-1);
266fa9e4066Sahrens }
267fa9e4066Sahrens 
26899653d4eSeschrock static int
26999653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
27099653d4eSeschrock     va_list ap)
27199653d4eSeschrock {
27299653d4eSeschrock 	switch (error) {
27399653d4eSeschrock 	case EPERM:
27499653d4eSeschrock 	case EACCES:
27599653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
27699653d4eSeschrock 		return (-1);
27799653d4eSeschrock 
278ecd6cf80Smarks 	case ECANCELED:
279ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
280ecd6cf80Smarks 		return (-1);
281ecd6cf80Smarks 
28299653d4eSeschrock 	case EIO:
28399653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
28499653d4eSeschrock 		return (-1);
28599653d4eSeschrock 
28699653d4eSeschrock 	case EINTR:
28799653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
28899653d4eSeschrock 		return (-1);
28999653d4eSeschrock 	}
29099653d4eSeschrock 
29199653d4eSeschrock 	return (0);
29299653d4eSeschrock }
29399653d4eSeschrock 
294ece3d9b3Slling int
295ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
296ece3d9b3Slling {
297ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
298ece3d9b3Slling }
299ece3d9b3Slling 
30099653d4eSeschrock /*PRINTFLIKE3*/
30199653d4eSeschrock int
302ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	va_list ap;
305fa9e4066Sahrens 
306fa9e4066Sahrens 	va_start(ap, fmt);
307fa9e4066Sahrens 
30899653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
30999653d4eSeschrock 		va_end(ap);
31099653d4eSeschrock 		return (-1);
311fa9e4066Sahrens 	}
312fa9e4066Sahrens 
313fa9e4066Sahrens 
31499653d4eSeschrock 	switch (error) {
31599653d4eSeschrock 	case ENXIO:
31699653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
31799653d4eSeschrock 		break;
31899653d4eSeschrock 
31999653d4eSeschrock 	case ENOENT:
32099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32199653d4eSeschrock 		    "dataset does not exist"));
32299653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
32399653d4eSeschrock 		break;
32499653d4eSeschrock 
32599653d4eSeschrock 	case ENOSPC:
32699653d4eSeschrock 	case EDQUOT:
32799653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
32899653d4eSeschrock 		return (-1);
32999653d4eSeschrock 
33099653d4eSeschrock 	case EEXIST:
33199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33299653d4eSeschrock 		    "dataset already exists"));
33399653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
33499653d4eSeschrock 		break;
33599653d4eSeschrock 
33699653d4eSeschrock 	case EBUSY:
33799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33899653d4eSeschrock 		    "dataset is busy"));
33999653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
34099653d4eSeschrock 		break;
341ecd6cf80Smarks 	case EROFS:
342ecd6cf80Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
343ecd6cf80Smarks 		    "snapshot permissions cannot be modified"));
344ecd6cf80Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
345ecd6cf80Smarks 		break;
346b7661cccSmmusante 	case ENAMETOOLONG:
347b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
348b7661cccSmmusante 		break;
34999653d4eSeschrock 	default:
35099653d4eSeschrock 		zfs_error_aux(hdl, strerror(errno));
35199653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
35299653d4eSeschrock 		break;
35399653d4eSeschrock 	}
35499653d4eSeschrock 
35599653d4eSeschrock 	va_end(ap);
35699653d4eSeschrock 	return (-1);
357fa9e4066Sahrens }
358fa9e4066Sahrens 
359ece3d9b3Slling int
360ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
361ece3d9b3Slling {
362ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
363ece3d9b3Slling }
364ece3d9b3Slling 
36599653d4eSeschrock /*PRINTFLIKE3*/
36699653d4eSeschrock int
367ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
368fa9e4066Sahrens {
36999653d4eSeschrock 	va_list ap;
37099653d4eSeschrock 
37199653d4eSeschrock 	va_start(ap, fmt);
37299653d4eSeschrock 
37399653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
37499653d4eSeschrock 		va_end(ap);
37599653d4eSeschrock 		return (-1);
37699653d4eSeschrock 	}
37799653d4eSeschrock 
37899653d4eSeschrock 	switch (error) {
37999653d4eSeschrock 	case ENODEV:
38099653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
38199653d4eSeschrock 		break;
38299653d4eSeschrock 
38399653d4eSeschrock 	case ENOENT:
384b1b8ab34Slling 		zfs_error_aux(hdl,
385b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
38699653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
38799653d4eSeschrock 		break;
38899653d4eSeschrock 
38999653d4eSeschrock 	case EEXIST:
39099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39199653d4eSeschrock 		    "pool already exists"));
39299653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
39399653d4eSeschrock 		break;
39499653d4eSeschrock 
39599653d4eSeschrock 	case EBUSY:
39699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
39799653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
39899653d4eSeschrock 		break;
39999653d4eSeschrock 
40099653d4eSeschrock 	case ENXIO:
40199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40299653d4eSeschrock 		    "one or more devices is currently unavailable"));
40399653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
40499653d4eSeschrock 		break;
40599653d4eSeschrock 
40699653d4eSeschrock 	case ENAMETOOLONG:
40799653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
40899653d4eSeschrock 		break;
40999653d4eSeschrock 
410b1b8ab34Slling 	case ENOTSUP:
411b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
412b1b8ab34Slling 		break;
413b1b8ab34Slling 
414b1b8ab34Slling 	case EINVAL:
415b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
416b1b8ab34Slling 		break;
417b1b8ab34Slling 
418ecd6cf80Smarks 	case ENOSPC:
419ecd6cf80Smarks 	case EDQUOT:
420ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
421ecd6cf80Smarks 		return (-1);
422ecd6cf80Smarks 
42399653d4eSeschrock 	default:
42499653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
42599653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
42699653d4eSeschrock 	}
42799653d4eSeschrock 
42899653d4eSeschrock 	va_end(ap);
42999653d4eSeschrock 	return (-1);
430fa9e4066Sahrens }
431fa9e4066Sahrens 
432fa9e4066Sahrens /*
433fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
434fa9e4066Sahrens  */
43599653d4eSeschrock int
43699653d4eSeschrock no_memory(libzfs_handle_t *hdl)
437fa9e4066Sahrens {
43899653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
439fa9e4066Sahrens }
440fa9e4066Sahrens 
441fa9e4066Sahrens /*
442fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
443fa9e4066Sahrens  */
444fa9e4066Sahrens void *
44599653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
446fa9e4066Sahrens {
447fa9e4066Sahrens 	void *data;
448fa9e4066Sahrens 
449fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
45099653d4eSeschrock 		(void) no_memory(hdl);
451fa9e4066Sahrens 
452fa9e4066Sahrens 	return (data);
453fa9e4066Sahrens }
454fa9e4066Sahrens 
455e9dbad6fSeschrock /*
456e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
457e9dbad6fSeschrock  */
458e9dbad6fSeschrock void *
459e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
460e9dbad6fSeschrock {
461e9dbad6fSeschrock 	void *ret;
462e9dbad6fSeschrock 
463e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
464e9dbad6fSeschrock 		(void) no_memory(hdl);
465e9dbad6fSeschrock 		free(ptr);
466e9dbad6fSeschrock 		return (NULL);
467e9dbad6fSeschrock 	}
468e9dbad6fSeschrock 
469e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
470e9dbad6fSeschrock 	return (ret);
471e9dbad6fSeschrock }
472e9dbad6fSeschrock 
473fa9e4066Sahrens /*
474fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
475fa9e4066Sahrens  */
476fa9e4066Sahrens char *
47799653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
478fa9e4066Sahrens {
479fa9e4066Sahrens 	char *ret;
480fa9e4066Sahrens 
481fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
48299653d4eSeschrock 		(void) no_memory(hdl);
483fa9e4066Sahrens 
484fa9e4066Sahrens 	return (ret);
485fa9e4066Sahrens }
486fa9e4066Sahrens 
487fa9e4066Sahrens /*
488fa9e4066Sahrens  * Convert a number to an appropriately human-readable output.
489fa9e4066Sahrens  */
490fa9e4066Sahrens void
491fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
492fa9e4066Sahrens {
493fa9e4066Sahrens 	uint64_t n = num;
494fa9e4066Sahrens 	int index = 0;
495fa9e4066Sahrens 	char u;
496fa9e4066Sahrens 
497fa9e4066Sahrens 	while (n >= 1024) {
4985c709891Seschrock 		n /= 1024;
499fa9e4066Sahrens 		index++;
500fa9e4066Sahrens 	}
501fa9e4066Sahrens 
502fa9e4066Sahrens 	u = " KMGTPE"[index];
503fa9e4066Sahrens 
5045c709891Seschrock 	if (index == 0) {
505fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5065c709891Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5075c709891Seschrock 		/*
5085c709891Seschrock 		 * If this is an even multiple of the base, always display
5095c709891Seschrock 		 * without any decimal precision.
5105c709891Seschrock 		 */
511fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5125c709891Seschrock 	} else {
5135c709891Seschrock 		/*
5145c709891Seschrock 		 * We want to choose a precision that reflects the best choice
5155c709891Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5165c709891Seschrock 		 * we have numbers that are very close to an order of magnitude.
5175c709891Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5185c709891Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5195c709891Seschrock 		 * develop some complex heuristics for this, but it's much
5205c709891Seschrock 		 * easier just to try each combination in turn.
5215c709891Seschrock 		 */
5225c709891Seschrock 		int i;
5235c709891Seschrock 		for (i = 2; i >= 0; i--) {
5243d7072f8Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5253d7072f8Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5265c709891Seschrock 				break;
5275c709891Seschrock 		}
5285c709891Seschrock 	}
529fa9e4066Sahrens }
53099653d4eSeschrock 
53199653d4eSeschrock void
53299653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
53399653d4eSeschrock {
53499653d4eSeschrock 	hdl->libzfs_printerr = printerr;
53599653d4eSeschrock }
53699653d4eSeschrock 
53799653d4eSeschrock libzfs_handle_t *
53899653d4eSeschrock libzfs_init(void)
53999653d4eSeschrock {
54099653d4eSeschrock 	libzfs_handle_t *hdl;
54199653d4eSeschrock 
54299653d4eSeschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
54399653d4eSeschrock 		return (NULL);
54499653d4eSeschrock 	}
54599653d4eSeschrock 
546c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
54799653d4eSeschrock 		free(hdl);
54899653d4eSeschrock 		return (NULL);
54999653d4eSeschrock 	}
55099653d4eSeschrock 
55199653d4eSeschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
55299653d4eSeschrock 		(void) close(hdl->libzfs_fd);
55399653d4eSeschrock 		free(hdl);
55499653d4eSeschrock 		return (NULL);
55599653d4eSeschrock 	}
55699653d4eSeschrock 
55799653d4eSeschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
55899653d4eSeschrock 
55991ebeef5Sahrens 	zfs_prop_init();
560*990b4856Slling 	zpool_prop_init();
56191ebeef5Sahrens 
56299653d4eSeschrock 	return (hdl);
56399653d4eSeschrock }
56499653d4eSeschrock 
56599653d4eSeschrock void
56699653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
56799653d4eSeschrock {
56899653d4eSeschrock 	(void) close(hdl->libzfs_fd);
56999653d4eSeschrock 	if (hdl->libzfs_mnttab)
57099653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
57199653d4eSeschrock 	if (hdl->libzfs_sharetab)
57299653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
57367331909Sdougm 	zfs_uninit_libshare(hdl);
574ecd6cf80Smarks 	if (hdl->libzfs_log_str)
575ecd6cf80Smarks 		(void) free(hdl->libzfs_log_str);
57699653d4eSeschrock 	namespace_clear(hdl);
57799653d4eSeschrock 	free(hdl);
57899653d4eSeschrock }
57999653d4eSeschrock 
58099653d4eSeschrock libzfs_handle_t *
58199653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
58299653d4eSeschrock {
58399653d4eSeschrock 	return (zhp->zpool_hdl);
58499653d4eSeschrock }
58599653d4eSeschrock 
58699653d4eSeschrock libzfs_handle_t *
58799653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
58899653d4eSeschrock {
58999653d4eSeschrock 	return (zhp->zfs_hdl);
59099653d4eSeschrock }
591e9dbad6fSeschrock 
5925aba80dbSck /*
5935aba80dbSck  * Given a name, determine whether or not it's a valid path
5945aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
5955aba80dbSck  * to match the device number.  If not, treat the path as an
5965aba80dbSck  * fs/vol/snap name.
5975aba80dbSck  */
5985aba80dbSck zfs_handle_t *
5995aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6005aba80dbSck {
6015aba80dbSck 	struct stat64 statbuf;
6025aba80dbSck 	struct extmnttab entry;
6035aba80dbSck 	int ret;
6045aba80dbSck 
6055aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6065aba80dbSck 		/*
6075aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
6085aba80dbSck 		 */
6095aba80dbSck 		return (zfs_open(hdl, path, argtype));
6105aba80dbSck 	}
6115aba80dbSck 
6125aba80dbSck 	if (stat64(path, &statbuf) != 0) {
6135aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6145aba80dbSck 		return (NULL);
6155aba80dbSck 	}
6165aba80dbSck 
6175aba80dbSck 	rewind(hdl->libzfs_mnttab);
6185aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6195aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6205aba80dbSck 		    statbuf.st_dev) {
6215aba80dbSck 			break;
6225aba80dbSck 		}
6235aba80dbSck 	}
6245aba80dbSck 	if (ret != 0) {
6255aba80dbSck 		return (NULL);
6265aba80dbSck 	}
6275aba80dbSck 
6285aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6295aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6305aba80dbSck 		    path);
6315aba80dbSck 		return (NULL);
6325aba80dbSck 	}
6335aba80dbSck 
6345aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6355aba80dbSck }
6365aba80dbSck 
637e9dbad6fSeschrock /*
638e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
639e9dbad6fSeschrock  * an ioctl().
640e9dbad6fSeschrock  */
641e9dbad6fSeschrock int
642e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
643e9dbad6fSeschrock {
644e9dbad6fSeschrock 	if (len == 0)
645a2eea2e1Sahrens 		len = 2048;
646e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
647e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
648e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
649e9dbad6fSeschrock 		return (-1);
650e9dbad6fSeschrock 
651e9dbad6fSeschrock 	return (0);
652e9dbad6fSeschrock }
653e9dbad6fSeschrock 
654e9dbad6fSeschrock /*
655e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
656e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
657e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
658e9dbad6fSeschrock  */
659e9dbad6fSeschrock int
660e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
661e9dbad6fSeschrock {
662e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
663e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
664e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
665e9dbad6fSeschrock 	    == NULL)
666e9dbad6fSeschrock 		return (-1);
667e9dbad6fSeschrock 
668e9dbad6fSeschrock 	return (0);
669e9dbad6fSeschrock }
670e9dbad6fSeschrock 
671e9dbad6fSeschrock /*
672a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
673e9dbad6fSeschrock  */
674e9dbad6fSeschrock void
675e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
676e9dbad6fSeschrock {
677*990b4856Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
678e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
679e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
680e9dbad6fSeschrock }
681e9dbad6fSeschrock 
682*990b4856Slling static int
683*990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
684*990b4856Slling     nvlist_t *nvl)
685e9dbad6fSeschrock {
686e9dbad6fSeschrock 	char *packed;
687e9dbad6fSeschrock 	size_t len;
688e9dbad6fSeschrock 
689e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
690e9dbad6fSeschrock 
691e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
692e9dbad6fSeschrock 		return (-1);
693e9dbad6fSeschrock 
694e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
695e9dbad6fSeschrock 
696*990b4856Slling 	*outnv = (uint64_t)(uintptr_t)packed;
697*990b4856Slling 	*outlen = len;
698e9dbad6fSeschrock 
699e9dbad6fSeschrock 	return (0);
700e9dbad6fSeschrock }
701e9dbad6fSeschrock 
702*990b4856Slling int
703*990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
704*990b4856Slling {
705*990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
706*990b4856Slling 	    &zc->zc_nvlist_conf_size, nvl));
707*990b4856Slling }
708*990b4856Slling 
709*990b4856Slling int
710*990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
711*990b4856Slling {
712*990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
713*990b4856Slling 	    &zc->zc_nvlist_src_size, nvl));
714*990b4856Slling }
715*990b4856Slling 
716e9dbad6fSeschrock /*
717e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
718e9dbad6fSeschrock  */
719e9dbad6fSeschrock int
720e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
721e9dbad6fSeschrock {
722e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
723e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
724e9dbad6fSeschrock 		return (no_memory(hdl));
725e9dbad6fSeschrock 
726e9dbad6fSeschrock 	return (0);
727e9dbad6fSeschrock }
728b1b8ab34Slling 
729*990b4856Slling int
730*990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
731*990b4856Slling {
732*990b4856Slling 	int error;
733*990b4856Slling 
734*990b4856Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
735*990b4856Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
736*990b4856Slling 	if (hdl->libzfs_log_str) {
737*990b4856Slling 		free(hdl->libzfs_log_str);
738*990b4856Slling 		hdl->libzfs_log_str = NULL;
739*990b4856Slling 	}
740*990b4856Slling 	zc->zc_history = 0;
741*990b4856Slling 
742*990b4856Slling 	return (error);
743*990b4856Slling }
744*990b4856Slling 
745*990b4856Slling /*
746*990b4856Slling  * ================================================================
747*990b4856Slling  * API shared by zfs and zpool property management
748*990b4856Slling  * ================================================================
749*990b4856Slling  */
750*990b4856Slling 
751b1b8ab34Slling static void
752*990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
753b1b8ab34Slling {
754*990b4856Slling 	zprop_list_t *pl = cbp->cb_proplist;
755b1b8ab34Slling 	int i;
756b1b8ab34Slling 	char *title;
757b1b8ab34Slling 	size_t len;
758b1b8ab34Slling 
759b1b8ab34Slling 	cbp->cb_first = B_FALSE;
760b1b8ab34Slling 	if (cbp->cb_scripted)
761b1b8ab34Slling 		return;
762b1b8ab34Slling 
763b1b8ab34Slling 	/*
764b1b8ab34Slling 	 * Start with the length of the column headers.
765b1b8ab34Slling 	 */
766b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
767b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
768b1b8ab34Slling 	    "PROPERTY"));
769b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
770b1b8ab34Slling 	    "VALUE"));
771b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
772b1b8ab34Slling 	    "SOURCE"));
773b1b8ab34Slling 
774b1b8ab34Slling 	/*
775b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
776b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
777b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
778b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
779b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
780b1b8ab34Slling 	 * if the name of the property is much longer the any values we find.
781b1b8ab34Slling 	 */
782b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
783b1b8ab34Slling 		/*
784b1b8ab34Slling 		 * 'PROPERTY' column
785b1b8ab34Slling 		 */
786*990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
787*990b4856Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
788*990b4856Slling 			    zpool_prop_to_name(pl->pl_prop) :
789*990b4856Slling 			    zfs_prop_to_name(pl->pl_prop);
790*990b4856Slling 
791*990b4856Slling 			len = strlen(propname);
792b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
793b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
794b1b8ab34Slling 		} else {
795b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
796b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
797b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
798b1b8ab34Slling 		}
799b1b8ab34Slling 
800b1b8ab34Slling 		/*
801b1b8ab34Slling 		 * 'VALUE' column
802b1b8ab34Slling 		 */
803b1b8ab34Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
804b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
805b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
806b1b8ab34Slling 
807b1b8ab34Slling 		/*
808b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
809b1b8ab34Slling 		 */
810*990b4856Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
811*990b4856Slling 		    ZFS_PROP_NAME) &&
812b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
813b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
814b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
815b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
816b1b8ab34Slling 		}
817b1b8ab34Slling 	}
818b1b8ab34Slling 
819b1b8ab34Slling 	/*
820b1b8ab34Slling 	 * Now go through and print the headers.
821b1b8ab34Slling 	 */
822b1b8ab34Slling 	for (i = 0; i < 4; i++) {
823b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
824b1b8ab34Slling 		case GET_COL_NAME:
825b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
826b1b8ab34Slling 			break;
827b1b8ab34Slling 		case GET_COL_PROPERTY:
828b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
829b1b8ab34Slling 			break;
830b1b8ab34Slling 		case GET_COL_VALUE:
831b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
832b1b8ab34Slling 			break;
833b1b8ab34Slling 		case GET_COL_SOURCE:
834b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
835b1b8ab34Slling 			break;
836b1b8ab34Slling 		default:
837b1b8ab34Slling 			title = NULL;
838b1b8ab34Slling 		}
839b1b8ab34Slling 
840b1b8ab34Slling 		if (title != NULL) {
841b1b8ab34Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
842b1b8ab34Slling 				(void) printf("%s", title);
843b1b8ab34Slling 			else
844b1b8ab34Slling 				(void) printf("%-*s  ",
845b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
846b1b8ab34Slling 				    title);
847b1b8ab34Slling 		}
848b1b8ab34Slling 	}
849b1b8ab34Slling 	(void) printf("\n");
850b1b8ab34Slling }
851b1b8ab34Slling 
852b1b8ab34Slling /*
853b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
854b1b8ab34Slling  * structure.
855b1b8ab34Slling  */
856b1b8ab34Slling void
857*990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
858*990b4856Slling     const char *propname, const char *value, zprop_source_t sourcetype,
859b1b8ab34Slling     const char *source)
860b1b8ab34Slling {
861b1b8ab34Slling 	int i;
862b1b8ab34Slling 	const char *str;
863b1b8ab34Slling 	char buf[128];
864b1b8ab34Slling 
865b1b8ab34Slling 	/*
866b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
867b1b8ab34Slling 	 */
868b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
869b1b8ab34Slling 		return;
870b1b8ab34Slling 
871b1b8ab34Slling 	if (cbp->cb_first)
872*990b4856Slling 		zprop_print_headers(cbp, cbp->cb_type);
873b1b8ab34Slling 
874b1b8ab34Slling 	for (i = 0; i < 4; i++) {
875b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
876b1b8ab34Slling 		case GET_COL_NAME:
877b1b8ab34Slling 			str = name;
878b1b8ab34Slling 			break;
879b1b8ab34Slling 
880b1b8ab34Slling 		case GET_COL_PROPERTY:
881b1b8ab34Slling 			str = propname;
882b1b8ab34Slling 			break;
883b1b8ab34Slling 
884b1b8ab34Slling 		case GET_COL_VALUE:
885b1b8ab34Slling 			str = value;
886b1b8ab34Slling 			break;
887b1b8ab34Slling 
888b1b8ab34Slling 		case GET_COL_SOURCE:
889b1b8ab34Slling 			switch (sourcetype) {
890*990b4856Slling 			case ZPROP_SRC_NONE:
891b1b8ab34Slling 				str = "-";
892b1b8ab34Slling 				break;
893b1b8ab34Slling 
894*990b4856Slling 			case ZPROP_SRC_DEFAULT:
895b1b8ab34Slling 				str = "default";
896b1b8ab34Slling 				break;
897b1b8ab34Slling 
898*990b4856Slling 			case ZPROP_SRC_LOCAL:
899b1b8ab34Slling 				str = "local";
900b1b8ab34Slling 				break;
901b1b8ab34Slling 
902*990b4856Slling 			case ZPROP_SRC_TEMPORARY:
903b1b8ab34Slling 				str = "temporary";
904b1b8ab34Slling 				break;
905b1b8ab34Slling 
906*990b4856Slling 			case ZPROP_SRC_INHERITED:
907b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
908b1b8ab34Slling 				    "inherited from %s", source);
909b1b8ab34Slling 				str = buf;
910b1b8ab34Slling 				break;
911b1b8ab34Slling 			}
912b1b8ab34Slling 			break;
913b1b8ab34Slling 
914b1b8ab34Slling 		default:
915b1b8ab34Slling 			continue;
916b1b8ab34Slling 		}
917b1b8ab34Slling 
918b1b8ab34Slling 		if (cbp->cb_columns[i + 1] == 0)
919b1b8ab34Slling 			(void) printf("%s", str);
920b1b8ab34Slling 		else if (cbp->cb_scripted)
921b1b8ab34Slling 			(void) printf("%s\t", str);
922b1b8ab34Slling 		else
923b1b8ab34Slling 			(void) printf("%-*s  ",
924b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
925b1b8ab34Slling 			    str);
926b1b8ab34Slling 
927b1b8ab34Slling 	}
928b1b8ab34Slling 
929b1b8ab34Slling 	(void) printf("\n");
930b1b8ab34Slling }
931ecd6cf80Smarks 
932*990b4856Slling /*
933*990b4856Slling  * Given a numeric suffix, convert the value into a number of bits that the
934*990b4856Slling  * resulting value must be shifted.
935*990b4856Slling  */
936*990b4856Slling static int
937*990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf)
938*990b4856Slling {
939*990b4856Slling 	const char *ends = "BKMGTPEZ";
940*990b4856Slling 	int i;
941*990b4856Slling 
942*990b4856Slling 	if (buf[0] == '\0')
943*990b4856Slling 		return (0);
944*990b4856Slling 	for (i = 0; i < strlen(ends); i++) {
945*990b4856Slling 		if (toupper(buf[0]) == ends[i])
946*990b4856Slling 			break;
947*990b4856Slling 	}
948*990b4856Slling 	if (i == strlen(ends)) {
949*990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950*990b4856Slling 		    "invalid numeric suffix '%s'"), buf);
951*990b4856Slling 		return (-1);
952*990b4856Slling 	}
953*990b4856Slling 
954*990b4856Slling 	/*
955*990b4856Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
956*990b4856Slling 	 * allow 'BB' - that's just weird.
957*990b4856Slling 	 */
958*990b4856Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
959*990b4856Slling 	    toupper(buf[0]) != 'B'))
960*990b4856Slling 		return (10*i);
961*990b4856Slling 
962*990b4856Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
963*990b4856Slling 	    "invalid numeric suffix '%s'"), buf);
964*990b4856Slling 	return (-1);
965*990b4856Slling }
966*990b4856Slling 
967*990b4856Slling /*
968*990b4856Slling  * Convert a string of the form '100G' into a real number.  Used when setting
969*990b4856Slling  * properties or creating a volume.  'buf' is used to place an extended error
970*990b4856Slling  * message for the caller to use.
971*990b4856Slling  */
972ecd6cf80Smarks int
973*990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
974ecd6cf80Smarks {
975*990b4856Slling 	char *end;
976*990b4856Slling 	int shift;
977ecd6cf80Smarks 
978*990b4856Slling 	*num = 0;
979*990b4856Slling 
980*990b4856Slling 	/* Check to see if this looks like a number.  */
981*990b4856Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
982*990b4856Slling 		if (hdl)
983*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984*990b4856Slling 			    "bad numeric value '%s'"), value);
985*990b4856Slling 		return (-1);
986ecd6cf80Smarks 	}
987ecd6cf80Smarks 
988*990b4856Slling 	/* Rely on stroll() to process the numeric portion.  */
989*990b4856Slling 	errno = 0;
990*990b4856Slling 	*num = strtoll(value, &end, 10);
991*990b4856Slling 
992*990b4856Slling 	/*
993*990b4856Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
994*990b4856Slling 	 * in a 64-bit value.
995*990b4856Slling 	 */
996*990b4856Slling 	if (errno == ERANGE) {
997*990b4856Slling 		if (hdl)
998*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
999*990b4856Slling 			    "numeric value is too large"));
1000*990b4856Slling 		return (-1);
1001*990b4856Slling 	}
1002*990b4856Slling 
1003*990b4856Slling 	/*
1004*990b4856Slling 	 * If we have a decimal value, then do the computation with floating
1005*990b4856Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1006*990b4856Slling 	 */
1007*990b4856Slling 	if (*end == '.') {
1008*990b4856Slling 		double fval = strtod(value, &end);
1009*990b4856Slling 
1010*990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1011*990b4856Slling 			return (-1);
1012*990b4856Slling 
1013*990b4856Slling 		fval *= pow(2, shift);
1014*990b4856Slling 
1015*990b4856Slling 		if (fval > UINT64_MAX) {
1016*990b4856Slling 			if (hdl)
1017*990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018*990b4856Slling 				    "numeric value is too large"));
1019*990b4856Slling 			return (-1);
1020*990b4856Slling 		}
1021*990b4856Slling 
1022*990b4856Slling 		*num = (uint64_t)fval;
1023*990b4856Slling 	} else {
1024*990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1025*990b4856Slling 			return (-1);
1026*990b4856Slling 
1027*990b4856Slling 		/* Check for overflow */
1028*990b4856Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1029*990b4856Slling 			if (hdl)
1030*990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031*990b4856Slling 				    "numeric value is too large"));
1032*990b4856Slling 			return (-1);
1033*990b4856Slling 		}
1034*990b4856Slling 
1035*990b4856Slling 		*num <<= shift;
1036*990b4856Slling 	}
1037*990b4856Slling 
1038*990b4856Slling 	return (0);
1039*990b4856Slling }
1040*990b4856Slling 
1041*990b4856Slling /*
1042*990b4856Slling  * Given a propname=value nvpair to set, parse any numeric properties
1043*990b4856Slling  * (index, boolean, etc) if they are specified as strings and add the
1044*990b4856Slling  * resulting nvpair to the returned nvlist.
1045*990b4856Slling  *
1046*990b4856Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1047*990b4856Slling  * We want the user to be able to ignore this fact and specify properties
1048*990b4856Slling  * as native values (numbers, for example) or as strings (to simplify
1049*990b4856Slling  * command line utilities).  This also handles converting index types
1050*990b4856Slling  * (compression, checksum, etc) from strings to their on-disk index.
1051*990b4856Slling  */
1052*990b4856Slling int
1053*990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1054*990b4856Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1055*990b4856Slling     const char *errbuf)
1056*990b4856Slling {
1057*990b4856Slling 	data_type_t datatype = nvpair_type(elem);
1058*990b4856Slling 	zprop_type_t proptype;
1059*990b4856Slling 	const char *propname;
1060*990b4856Slling 	char *value;
1061*990b4856Slling 	boolean_t isnone = B_FALSE;
1062*990b4856Slling 
1063*990b4856Slling 	if (type == ZFS_TYPE_POOL) {
1064*990b4856Slling 		proptype = zpool_prop_get_type(prop);
1065*990b4856Slling 		propname = zpool_prop_to_name(prop);
1066*990b4856Slling 	} else {
1067*990b4856Slling 		proptype = zfs_prop_get_type(prop);
1068*990b4856Slling 		propname = zfs_prop_to_name(prop);
1069*990b4856Slling 	}
1070*990b4856Slling 
1071*990b4856Slling 	/*
1072*990b4856Slling 	 * Convert any properties to the internal DSL value types.
1073*990b4856Slling 	 */
1074*990b4856Slling 	*svalp = NULL;
1075*990b4856Slling 	*ivalp = 0;
1076*990b4856Slling 
1077*990b4856Slling 	switch (proptype) {
1078*990b4856Slling 	case PROP_TYPE_STRING:
1079*990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1080*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1081*990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1082*990b4856Slling 			goto error;
1083*990b4856Slling 		}
1084*990b4856Slling 		(void) nvpair_value_string(elem, svalp);
1085*990b4856Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1086*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1087*990b4856Slling 			    "'%s' is too long"), nvpair_name(elem));
1088*990b4856Slling 			goto error;
1089*990b4856Slling 		}
1090*990b4856Slling 		break;
1091*990b4856Slling 
1092*990b4856Slling 	case PROP_TYPE_NUMBER:
1093*990b4856Slling 		if (datatype == DATA_TYPE_STRING) {
1094*990b4856Slling 			(void) nvpair_value_string(elem, &value);
1095*990b4856Slling 			if (strcmp(value, "none") == 0) {
1096*990b4856Slling 				isnone = B_TRUE;
1097*990b4856Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1098*990b4856Slling 			    != 0) {
1099*990b4856Slling 				goto error;
1100*990b4856Slling 			}
1101*990b4856Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1102*990b4856Slling 			(void) nvpair_value_uint64(elem, ivalp);
1103*990b4856Slling 		} else {
1104*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1105*990b4856Slling 			    "'%s' must be a number"), nvpair_name(elem));
1106*990b4856Slling 			goto error;
1107*990b4856Slling 		}
1108*990b4856Slling 
1109*990b4856Slling 		/*
1110*990b4856Slling 		 * Quota special: force 'none' and don't allow 0.
1111*990b4856Slling 		 */
1112*990b4856Slling 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 &&
1113*990b4856Slling 		    !isnone && prop == ZFS_PROP_QUOTA) {
1114*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115*990b4856Slling 			    "use 'none' to disable quota"));
1116*990b4856Slling 			goto error;
1117*990b4856Slling 		}
1118*990b4856Slling 		break;
1119*990b4856Slling 
1120*990b4856Slling 	case PROP_TYPE_INDEX:
1121*990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1122*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1123*990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1124*990b4856Slling 			goto error;
1125*990b4856Slling 		}
1126*990b4856Slling 
1127*990b4856Slling 		(void) nvpair_value_string(elem, &value);
1128*990b4856Slling 
1129*990b4856Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1130*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131*990b4856Slling 			    "'%s' must be one of '%s'"), propname,
1132*990b4856Slling 			    zprop_values(prop, type));
1133*990b4856Slling 			goto error;
1134*990b4856Slling 		}
1135*990b4856Slling 		break;
1136*990b4856Slling 
1137*990b4856Slling 	default:
1138*990b4856Slling 		abort();
1139*990b4856Slling 	}
1140*990b4856Slling 
1141*990b4856Slling 	/*
1142*990b4856Slling 	 * Add the result to our return set of properties.
1143*990b4856Slling 	 */
1144*990b4856Slling 	if (*svalp != NULL) {
1145*990b4856Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1146*990b4856Slling 			(void) no_memory(hdl);
1147*990b4856Slling 			return (-1);
1148*990b4856Slling 		}
1149*990b4856Slling 	} else {
1150*990b4856Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1151*990b4856Slling 			(void) no_memory(hdl);
1152*990b4856Slling 			return (-1);
1153*990b4856Slling 		}
1154*990b4856Slling 	}
1155*990b4856Slling 
1156*990b4856Slling 	return (0);
1157*990b4856Slling error:
1158*990b4856Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1159*990b4856Slling 	return (-1);
1160*990b4856Slling }
1161*990b4856Slling 
1162*990b4856Slling /*
1163*990b4856Slling  * Given a comma-separated list of properties, construct a property list
1164*990b4856Slling  * containing both user-defined and native properties.  This function will
1165*990b4856Slling  * return a NULL list if 'all' is specified, which can later be expanded
1166*990b4856Slling  * by zprop_expand_list().
1167*990b4856Slling  */
1168*990b4856Slling int
1169*990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1170*990b4856Slling     zfs_type_t type)
1171*990b4856Slling {
1172*990b4856Slling 	size_t len;
1173*990b4856Slling 	char *s, *p;
1174*990b4856Slling 	char c;
1175*990b4856Slling 	int prop;
1176*990b4856Slling 	zprop_list_t *entry;
1177*990b4856Slling 	zprop_list_t **last;
1178*990b4856Slling 
1179*990b4856Slling 	*listp = NULL;
1180*990b4856Slling 	last = listp;
1181*990b4856Slling 
1182*990b4856Slling 	/*
1183*990b4856Slling 	 * If 'all' is specified, return a NULL list.
1184*990b4856Slling 	 */
1185*990b4856Slling 	if (strcmp(props, "all") == 0)
1186*990b4856Slling 		return (0);
1187*990b4856Slling 
1188*990b4856Slling 	/*
1189*990b4856Slling 	 * If no props were specified, return an error.
1190*990b4856Slling 	 */
1191*990b4856Slling 	if (props[0] == '\0') {
1192*990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1193*990b4856Slling 		    "no properties specified"));
1194*990b4856Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1195*990b4856Slling 		    "bad property list")));
1196*990b4856Slling 	}
1197*990b4856Slling 
1198*990b4856Slling 	/*
1199*990b4856Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1200*990b4856Slling 	 * aliases makes this more effort than it's worth.
1201*990b4856Slling 	 */
1202*990b4856Slling 	s = props;
1203*990b4856Slling 	while (*s != '\0') {
1204*990b4856Slling 		if ((p = strchr(s, ',')) == NULL) {
1205*990b4856Slling 			len = strlen(s);
1206*990b4856Slling 			p = s + len;
1207*990b4856Slling 		} else {
1208*990b4856Slling 			len = p - s;
1209*990b4856Slling 		}
1210*990b4856Slling 
1211*990b4856Slling 		/*
1212*990b4856Slling 		 * Check for empty options.
1213*990b4856Slling 		 */
1214*990b4856Slling 		if (len == 0) {
1215*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1216*990b4856Slling 			    "empty property name"));
1217*990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1218*990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1219*990b4856Slling 		}
1220*990b4856Slling 
1221*990b4856Slling 		/*
1222*990b4856Slling 		 * Check all regular property names.
1223*990b4856Slling 		 */
1224*990b4856Slling 		c = s[len];
1225*990b4856Slling 		s[len] = '\0';
1226*990b4856Slling 		prop = zprop_name_to_prop(s, type);
1227*990b4856Slling 
1228*990b4856Slling 		if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1229*990b4856Slling 			prop = ZPROP_INVAL;
1230*990b4856Slling 
1231*990b4856Slling 		/*
1232*990b4856Slling 		 * When no property table entry can be found, return failure if
1233*990b4856Slling 		 * this is a pool property or if this isn't a user-defined
1234*990b4856Slling 		 * dataset property,
1235*990b4856Slling 		 */
1236*990b4856Slling 		if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1237*990b4856Slling 		    !zfs_prop_user(s))) {
1238*990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1239*990b4856Slling 			    "invalid property '%s'"), s);
1240*990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1241*990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1242*990b4856Slling 		}
1243*990b4856Slling 
1244*990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1245*990b4856Slling 			return (-1);
1246*990b4856Slling 
1247*990b4856Slling 		entry->pl_prop = prop;
1248*990b4856Slling 		if (prop == ZPROP_INVAL) {
1249*990b4856Slling 			if ((entry->pl_user_prop = zfs_strdup(hdl, s))
1250*990b4856Slling 			    == NULL) {
1251*990b4856Slling 				free(entry);
1252*990b4856Slling 				return (-1);
1253*990b4856Slling 			}
1254*990b4856Slling 			entry->pl_width = strlen(s);
1255*990b4856Slling 		} else {
1256*990b4856Slling 			entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1257*990b4856Slling 			    type);
1258*990b4856Slling 		}
1259*990b4856Slling 
1260*990b4856Slling 		*last = entry;
1261*990b4856Slling 		last = &entry->pl_next;
1262*990b4856Slling 
1263*990b4856Slling 		s = p;
1264*990b4856Slling 		if (c == ',')
1265*990b4856Slling 			s++;
1266*990b4856Slling 	}
1267*990b4856Slling 
1268*990b4856Slling 	return (0);
1269*990b4856Slling }
1270*990b4856Slling 
1271*990b4856Slling void
1272*990b4856Slling zprop_free_list(zprop_list_t *pl)
1273*990b4856Slling {
1274*990b4856Slling 	zprop_list_t *next;
1275*990b4856Slling 
1276*990b4856Slling 	while (pl != NULL) {
1277*990b4856Slling 		next = pl->pl_next;
1278*990b4856Slling 		free(pl->pl_user_prop);
1279*990b4856Slling 		free(pl);
1280*990b4856Slling 		pl = next;
1281*990b4856Slling 	}
1282*990b4856Slling }
1283*990b4856Slling 
1284*990b4856Slling typedef struct expand_data {
1285*990b4856Slling 	zprop_list_t	**last;
1286*990b4856Slling 	libzfs_handle_t	*hdl;
1287*990b4856Slling 	zfs_type_t type;
1288*990b4856Slling } expand_data_t;
1289*990b4856Slling 
1290*990b4856Slling int
1291*990b4856Slling zprop_expand_list_cb(int prop, void *cb)
1292*990b4856Slling {
1293*990b4856Slling 	zprop_list_t *entry;
1294*990b4856Slling 	expand_data_t *edp = cb;
1295*990b4856Slling 
1296*990b4856Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1297*990b4856Slling 		return (ZPROP_INVAL);
1298*990b4856Slling 
1299*990b4856Slling 	entry->pl_prop = prop;
1300*990b4856Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1301*990b4856Slling 	entry->pl_all = B_TRUE;
1302*990b4856Slling 
1303*990b4856Slling 	*(edp->last) = entry;
1304*990b4856Slling 	edp->last = &entry->pl_next;
1305*990b4856Slling 
1306*990b4856Slling 	return (ZPROP_CONT);
1307*990b4856Slling }
1308*990b4856Slling 
1309*990b4856Slling int
1310*990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1311*990b4856Slling {
1312*990b4856Slling 	zprop_list_t *entry;
1313*990b4856Slling 	zprop_list_t **last;
1314*990b4856Slling 	expand_data_t exp;
1315*990b4856Slling 
1316*990b4856Slling 	if (*plp == NULL) {
1317*990b4856Slling 		/*
1318*990b4856Slling 		 * If this is the very first time we've been called for an 'all'
1319*990b4856Slling 		 * specification, expand the list to include all native
1320*990b4856Slling 		 * properties.
1321*990b4856Slling 		 */
1322*990b4856Slling 		last = plp;
1323*990b4856Slling 
1324*990b4856Slling 		exp.last = last;
1325*990b4856Slling 		exp.hdl = hdl;
1326*990b4856Slling 		exp.type = type;
1327*990b4856Slling 
1328*990b4856Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1329*990b4856Slling 		    B_FALSE, type) == ZPROP_INVAL)
1330*990b4856Slling 			return (-1);
1331*990b4856Slling 
1332*990b4856Slling 		/*
1333*990b4856Slling 		 * Add 'name' to the beginning of the list, which is handled
1334*990b4856Slling 		 * specially.
1335*990b4856Slling 		 */
1336*990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1337*990b4856Slling 			return (-1);
1338*990b4856Slling 
1339*990b4856Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1340*990b4856Slling 		    ZFS_PROP_NAME;
1341*990b4856Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1342*990b4856Slling 		    &entry->pl_fixed, type);
1343*990b4856Slling 		entry->pl_all = B_TRUE;
1344*990b4856Slling 		entry->pl_next = *plp;
1345*990b4856Slling 		*plp = entry;
1346*990b4856Slling 	}
1347*990b4856Slling 	return (0);
1348*990b4856Slling }
1349*990b4856Slling 
1350*990b4856Slling int
1351*990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1352*990b4856Slling     zfs_type_t type)
1353*990b4856Slling {
1354*990b4856Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1355ecd6cf80Smarks }
1356