xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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>
40990b4856Slling #include <ctype.h>
41990b4856Slling #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"));
139*da6c28aaSamw 	case EZFS_UNSHARESMBFAILED:
140*da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
141*da6c28aaSamw 	case EZFS_SHARESMBFAILED:
142*da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
143ecd6cf80Smarks 	case EZFS_ISCSISVCUNAVAIL:
144ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN,
145ecd6cf80Smarks 		    "iscsitgt service need to be enabled by "
146ecd6cf80Smarks 		    "a privileged user"));
14799653d4eSeschrock 	case EZFS_DEVLINKS:
14899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
14999653d4eSeschrock 	case EZFS_PERM:
15099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
15199653d4eSeschrock 	case EZFS_NOSPC:
15299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
15399653d4eSeschrock 	case EZFS_IO:
15499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
15599653d4eSeschrock 	case EZFS_INTR:
15699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
15799653d4eSeschrock 	case EZFS_ISSPARE:
15899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
15999653d4eSeschrock 		    "spare"));
16099653d4eSeschrock 	case EZFS_INVALCONFIG:
16199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1623bb79becSeschrock 	case EZFS_RECURSIVE:
1633bb79becSeschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
16406eeb2adSek 	case EZFS_NOHISTORY:
16506eeb2adSek 		return (dgettext(TEXT_DOMAIN, "no history available"));
166f3861e1aSahl 	case EZFS_UNSHAREISCSIFAILED:
167f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
168f3861e1aSahl 		    "iscsitgtd failed request to unshare"));
169f3861e1aSahl 	case EZFS_SHAREISCSIFAILED:
170f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
171f3861e1aSahl 		    "iscsitgtd failed request to share"));
172b1b8ab34Slling 	case EZFS_POOLPROPS:
173b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
174b1b8ab34Slling 		    "pool properties"));
175b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
176b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
177b1b8ab34Slling 		    "on this type of pool"));
178b1b8ab34Slling 	case EZFS_POOL_INVALARG:
179b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
180b1b8ab34Slling 		    "this pool operation"));
181b7661cccSmmusante 	case EZFS_NAMETOOLONG:
182b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1838488aeb5Staylor 	case EZFS_OPENFAILED:
1848488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1858488aeb5Staylor 	case EZFS_NOCAP:
1868488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1878488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1888488aeb5Staylor 	case EZFS_LABELFAILED:
1898488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
190ecd6cf80Smarks 	case EZFS_BADWHO:
191ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
192ecd6cf80Smarks 	case EZFS_BADPERM:
193ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
194ecd6cf80Smarks 	case EZFS_BADPERMSET:
195ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
196ecd6cf80Smarks 	case EZFS_NODELEGATION:
197ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
198ecd6cf80Smarks 		    "disabled on pool"));
199ecd6cf80Smarks 	case EZFS_PERMRDONLY:
200ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
201ecd6cf80Smarks 		    " modified"));
20299653d4eSeschrock 	case EZFS_UNKNOWN:
20399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
20499653d4eSeschrock 	default:
205c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
206c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
20799653d4eSeschrock 	}
20899653d4eSeschrock }
20999653d4eSeschrock 
21099653d4eSeschrock /*PRINTFLIKE2*/
211fa9e4066Sahrens void
21299653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
213fa9e4066Sahrens {
214fa9e4066Sahrens 	va_list ap;
215fa9e4066Sahrens 
216fa9e4066Sahrens 	va_start(ap, fmt);
217fa9e4066Sahrens 
21899653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
21999653d4eSeschrock 	    fmt, ap);
22099653d4eSeschrock 	hdl->libzfs_desc_active = 1;
22199653d4eSeschrock 
22299653d4eSeschrock 	va_end(ap);
22399653d4eSeschrock }
22499653d4eSeschrock 
22599653d4eSeschrock static void
22699653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
22799653d4eSeschrock {
22899653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
22999653d4eSeschrock 	    fmt, ap);
23099653d4eSeschrock 	hdl->libzfs_error = error;
23199653d4eSeschrock 
23299653d4eSeschrock 	if (hdl->libzfs_desc_active)
23399653d4eSeschrock 		hdl->libzfs_desc_active = 0;
23499653d4eSeschrock 	else
23599653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
23699653d4eSeschrock 
23799653d4eSeschrock 	if (hdl->libzfs_printerr) {
23899653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
23999653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
24099653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
24199653d4eSeschrock 			abort();
24299653d4eSeschrock 		}
24399653d4eSeschrock 
24499653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
245b1b8ab34Slling 		    libzfs_error_description(hdl));
24699653d4eSeschrock 		if (error == EZFS_NOMEM)
24799653d4eSeschrock 			exit(1);
248fa9e4066Sahrens 	}
24999653d4eSeschrock }
25099653d4eSeschrock 
251ece3d9b3Slling int
252ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
253ece3d9b3Slling {
254ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
255ece3d9b3Slling }
256ece3d9b3Slling 
25799653d4eSeschrock /*PRINTFLIKE3*/
25899653d4eSeschrock int
259ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
26099653d4eSeschrock {
26199653d4eSeschrock 	va_list ap;
26299653d4eSeschrock 
26399653d4eSeschrock 	va_start(ap, fmt);
26499653d4eSeschrock 
26599653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
266fa9e4066Sahrens 
267fa9e4066Sahrens 	va_end(ap);
26899653d4eSeschrock 
26999653d4eSeschrock 	return (-1);
270fa9e4066Sahrens }
271fa9e4066Sahrens 
27299653d4eSeschrock static int
27399653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
27499653d4eSeschrock     va_list ap)
27599653d4eSeschrock {
27699653d4eSeschrock 	switch (error) {
27799653d4eSeschrock 	case EPERM:
27899653d4eSeschrock 	case EACCES:
27999653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
28099653d4eSeschrock 		return (-1);
28199653d4eSeschrock 
282ecd6cf80Smarks 	case ECANCELED:
283ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
284ecd6cf80Smarks 		return (-1);
285ecd6cf80Smarks 
28699653d4eSeschrock 	case EIO:
28799653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
28899653d4eSeschrock 		return (-1);
28999653d4eSeschrock 
29099653d4eSeschrock 	case EINTR:
29199653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
29299653d4eSeschrock 		return (-1);
29399653d4eSeschrock 	}
29499653d4eSeschrock 
29599653d4eSeschrock 	return (0);
29699653d4eSeschrock }
29799653d4eSeschrock 
298ece3d9b3Slling int
299ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
300ece3d9b3Slling {
301ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
302ece3d9b3Slling }
303ece3d9b3Slling 
30499653d4eSeschrock /*PRINTFLIKE3*/
30599653d4eSeschrock int
306ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
307fa9e4066Sahrens {
308fa9e4066Sahrens 	va_list ap;
309fa9e4066Sahrens 
310fa9e4066Sahrens 	va_start(ap, fmt);
311fa9e4066Sahrens 
31299653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
31399653d4eSeschrock 		va_end(ap);
31499653d4eSeschrock 		return (-1);
315fa9e4066Sahrens 	}
316fa9e4066Sahrens 
317fa9e4066Sahrens 
31899653d4eSeschrock 	switch (error) {
31999653d4eSeschrock 	case ENXIO:
32099653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
32199653d4eSeschrock 		break;
32299653d4eSeschrock 
32399653d4eSeschrock 	case ENOENT:
32499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32599653d4eSeschrock 		    "dataset does not exist"));
32699653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
32799653d4eSeschrock 		break;
32899653d4eSeschrock 
32999653d4eSeschrock 	case ENOSPC:
33099653d4eSeschrock 	case EDQUOT:
33199653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
33299653d4eSeschrock 		return (-1);
33399653d4eSeschrock 
33499653d4eSeschrock 	case EEXIST:
33599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33699653d4eSeschrock 		    "dataset already exists"));
33799653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
33899653d4eSeschrock 		break;
33999653d4eSeschrock 
34099653d4eSeschrock 	case EBUSY:
34199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34299653d4eSeschrock 		    "dataset is busy"));
34399653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
34499653d4eSeschrock 		break;
345ecd6cf80Smarks 	case EROFS:
346ecd6cf80Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
347ecd6cf80Smarks 		    "snapshot permissions cannot be modified"));
348ecd6cf80Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
349ecd6cf80Smarks 		break;
350b7661cccSmmusante 	case ENAMETOOLONG:
351b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
352b7661cccSmmusante 		break;
35399653d4eSeschrock 	default:
35499653d4eSeschrock 		zfs_error_aux(hdl, strerror(errno));
35599653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
35699653d4eSeschrock 		break;
35799653d4eSeschrock 	}
35899653d4eSeschrock 
35999653d4eSeschrock 	va_end(ap);
36099653d4eSeschrock 	return (-1);
361fa9e4066Sahrens }
362fa9e4066Sahrens 
363ece3d9b3Slling int
364ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
365ece3d9b3Slling {
366ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
367ece3d9b3Slling }
368ece3d9b3Slling 
36999653d4eSeschrock /*PRINTFLIKE3*/
37099653d4eSeschrock int
371ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
372fa9e4066Sahrens {
37399653d4eSeschrock 	va_list ap;
37499653d4eSeschrock 
37599653d4eSeschrock 	va_start(ap, fmt);
37699653d4eSeschrock 
37799653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
37899653d4eSeschrock 		va_end(ap);
37999653d4eSeschrock 		return (-1);
38099653d4eSeschrock 	}
38199653d4eSeschrock 
38299653d4eSeschrock 	switch (error) {
38399653d4eSeschrock 	case ENODEV:
38499653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
38599653d4eSeschrock 		break;
38699653d4eSeschrock 
38799653d4eSeschrock 	case ENOENT:
388b1b8ab34Slling 		zfs_error_aux(hdl,
389b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
39099653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
39199653d4eSeschrock 		break;
39299653d4eSeschrock 
39399653d4eSeschrock 	case EEXIST:
39499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39599653d4eSeschrock 		    "pool already exists"));
39699653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
39799653d4eSeschrock 		break;
39899653d4eSeschrock 
39999653d4eSeschrock 	case EBUSY:
40099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
40199653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
40299653d4eSeschrock 		break;
40399653d4eSeschrock 
40499653d4eSeschrock 	case ENXIO:
40599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40699653d4eSeschrock 		    "one or more devices is currently unavailable"));
40799653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
40899653d4eSeschrock 		break;
40999653d4eSeschrock 
41099653d4eSeschrock 	case ENAMETOOLONG:
41199653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
41299653d4eSeschrock 		break;
41399653d4eSeschrock 
414b1b8ab34Slling 	case ENOTSUP:
415b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
416b1b8ab34Slling 		break;
417b1b8ab34Slling 
418b1b8ab34Slling 	case EINVAL:
419b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
420b1b8ab34Slling 		break;
421b1b8ab34Slling 
422ecd6cf80Smarks 	case ENOSPC:
423ecd6cf80Smarks 	case EDQUOT:
424ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
425ecd6cf80Smarks 		return (-1);
426ecd6cf80Smarks 
42799653d4eSeschrock 	default:
42899653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
42999653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
43099653d4eSeschrock 	}
43199653d4eSeschrock 
43299653d4eSeschrock 	va_end(ap);
43399653d4eSeschrock 	return (-1);
434fa9e4066Sahrens }
435fa9e4066Sahrens 
436fa9e4066Sahrens /*
437fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
438fa9e4066Sahrens  */
43999653d4eSeschrock int
44099653d4eSeschrock no_memory(libzfs_handle_t *hdl)
441fa9e4066Sahrens {
44299653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
443fa9e4066Sahrens }
444fa9e4066Sahrens 
445fa9e4066Sahrens /*
446fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
447fa9e4066Sahrens  */
448fa9e4066Sahrens void *
44999653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
450fa9e4066Sahrens {
451fa9e4066Sahrens 	void *data;
452fa9e4066Sahrens 
453fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
45499653d4eSeschrock 		(void) no_memory(hdl);
455fa9e4066Sahrens 
456fa9e4066Sahrens 	return (data);
457fa9e4066Sahrens }
458fa9e4066Sahrens 
459e9dbad6fSeschrock /*
460e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
461e9dbad6fSeschrock  */
462e9dbad6fSeschrock void *
463e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
464e9dbad6fSeschrock {
465e9dbad6fSeschrock 	void *ret;
466e9dbad6fSeschrock 
467e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
468e9dbad6fSeschrock 		(void) no_memory(hdl);
469e9dbad6fSeschrock 		free(ptr);
470e9dbad6fSeschrock 		return (NULL);
471e9dbad6fSeschrock 	}
472e9dbad6fSeschrock 
473e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
474e9dbad6fSeschrock 	return (ret);
475e9dbad6fSeschrock }
476e9dbad6fSeschrock 
477fa9e4066Sahrens /*
478fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
479fa9e4066Sahrens  */
480fa9e4066Sahrens char *
48199653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
482fa9e4066Sahrens {
483fa9e4066Sahrens 	char *ret;
484fa9e4066Sahrens 
485fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
48699653d4eSeschrock 		(void) no_memory(hdl);
487fa9e4066Sahrens 
488fa9e4066Sahrens 	return (ret);
489fa9e4066Sahrens }
490fa9e4066Sahrens 
491fa9e4066Sahrens /*
492fa9e4066Sahrens  * Convert a number to an appropriately human-readable output.
493fa9e4066Sahrens  */
494fa9e4066Sahrens void
495fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
496fa9e4066Sahrens {
497fa9e4066Sahrens 	uint64_t n = num;
498fa9e4066Sahrens 	int index = 0;
499fa9e4066Sahrens 	char u;
500fa9e4066Sahrens 
501fa9e4066Sahrens 	while (n >= 1024) {
5025c709891Seschrock 		n /= 1024;
503fa9e4066Sahrens 		index++;
504fa9e4066Sahrens 	}
505fa9e4066Sahrens 
506fa9e4066Sahrens 	u = " KMGTPE"[index];
507fa9e4066Sahrens 
5085c709891Seschrock 	if (index == 0) {
509fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5105c709891Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5115c709891Seschrock 		/*
5125c709891Seschrock 		 * If this is an even multiple of the base, always display
5135c709891Seschrock 		 * without any decimal precision.
5145c709891Seschrock 		 */
515fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5165c709891Seschrock 	} else {
5175c709891Seschrock 		/*
5185c709891Seschrock 		 * We want to choose a precision that reflects the best choice
5195c709891Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5205c709891Seschrock 		 * we have numbers that are very close to an order of magnitude.
5215c709891Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5225c709891Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5235c709891Seschrock 		 * develop some complex heuristics for this, but it's much
5245c709891Seschrock 		 * easier just to try each combination in turn.
5255c709891Seschrock 		 */
5265c709891Seschrock 		int i;
5275c709891Seschrock 		for (i = 2; i >= 0; i--) {
5283d7072f8Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5293d7072f8Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5305c709891Seschrock 				break;
5315c709891Seschrock 		}
5325c709891Seschrock 	}
533fa9e4066Sahrens }
53499653d4eSeschrock 
53599653d4eSeschrock void
53699653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
53799653d4eSeschrock {
53899653d4eSeschrock 	hdl->libzfs_printerr = printerr;
53999653d4eSeschrock }
54099653d4eSeschrock 
54199653d4eSeschrock libzfs_handle_t *
54299653d4eSeschrock libzfs_init(void)
54399653d4eSeschrock {
54499653d4eSeschrock 	libzfs_handle_t *hdl;
54599653d4eSeschrock 
54699653d4eSeschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
54799653d4eSeschrock 		return (NULL);
54899653d4eSeschrock 	}
54999653d4eSeschrock 
550c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
55199653d4eSeschrock 		free(hdl);
55299653d4eSeschrock 		return (NULL);
55399653d4eSeschrock 	}
55499653d4eSeschrock 
55599653d4eSeschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
55699653d4eSeschrock 		(void) close(hdl->libzfs_fd);
55799653d4eSeschrock 		free(hdl);
55899653d4eSeschrock 		return (NULL);
55999653d4eSeschrock 	}
56099653d4eSeschrock 
56199653d4eSeschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
56299653d4eSeschrock 
56391ebeef5Sahrens 	zfs_prop_init();
564990b4856Slling 	zpool_prop_init();
56591ebeef5Sahrens 
56699653d4eSeschrock 	return (hdl);
56799653d4eSeschrock }
56899653d4eSeschrock 
56999653d4eSeschrock void
57099653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
57199653d4eSeschrock {
57299653d4eSeschrock 	(void) close(hdl->libzfs_fd);
57399653d4eSeschrock 	if (hdl->libzfs_mnttab)
57499653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
57599653d4eSeschrock 	if (hdl->libzfs_sharetab)
57699653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
57767331909Sdougm 	zfs_uninit_libshare(hdl);
578ecd6cf80Smarks 	if (hdl->libzfs_log_str)
579ecd6cf80Smarks 		(void) free(hdl->libzfs_log_str);
58099653d4eSeschrock 	namespace_clear(hdl);
58199653d4eSeschrock 	free(hdl);
58299653d4eSeschrock }
58399653d4eSeschrock 
58499653d4eSeschrock libzfs_handle_t *
58599653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
58699653d4eSeschrock {
58799653d4eSeschrock 	return (zhp->zpool_hdl);
58899653d4eSeschrock }
58999653d4eSeschrock 
59099653d4eSeschrock libzfs_handle_t *
59199653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
59299653d4eSeschrock {
59399653d4eSeschrock 	return (zhp->zfs_hdl);
59499653d4eSeschrock }
595e9dbad6fSeschrock 
5965aba80dbSck /*
5975aba80dbSck  * Given a name, determine whether or not it's a valid path
5985aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
5995aba80dbSck  * to match the device number.  If not, treat the path as an
6005aba80dbSck  * fs/vol/snap name.
6015aba80dbSck  */
6025aba80dbSck zfs_handle_t *
6035aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6045aba80dbSck {
6055aba80dbSck 	struct stat64 statbuf;
6065aba80dbSck 	struct extmnttab entry;
6075aba80dbSck 	int ret;
6085aba80dbSck 
6095aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6105aba80dbSck 		/*
6115aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
6125aba80dbSck 		 */
6135aba80dbSck 		return (zfs_open(hdl, path, argtype));
6145aba80dbSck 	}
6155aba80dbSck 
6165aba80dbSck 	if (stat64(path, &statbuf) != 0) {
6175aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6185aba80dbSck 		return (NULL);
6195aba80dbSck 	}
6205aba80dbSck 
6215aba80dbSck 	rewind(hdl->libzfs_mnttab);
6225aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6235aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6245aba80dbSck 		    statbuf.st_dev) {
6255aba80dbSck 			break;
6265aba80dbSck 		}
6275aba80dbSck 	}
6285aba80dbSck 	if (ret != 0) {
6295aba80dbSck 		return (NULL);
6305aba80dbSck 	}
6315aba80dbSck 
6325aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6335aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6345aba80dbSck 		    path);
6355aba80dbSck 		return (NULL);
6365aba80dbSck 	}
6375aba80dbSck 
6385aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6395aba80dbSck }
6405aba80dbSck 
641e9dbad6fSeschrock /*
642e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
643e9dbad6fSeschrock  * an ioctl().
644e9dbad6fSeschrock  */
645e9dbad6fSeschrock int
646e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
647e9dbad6fSeschrock {
648e9dbad6fSeschrock 	if (len == 0)
649a2eea2e1Sahrens 		len = 2048;
650e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
651e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
652e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
653e9dbad6fSeschrock 		return (-1);
654e9dbad6fSeschrock 
655e9dbad6fSeschrock 	return (0);
656e9dbad6fSeschrock }
657e9dbad6fSeschrock 
658e9dbad6fSeschrock /*
659e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
660e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
661e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
662e9dbad6fSeschrock  */
663e9dbad6fSeschrock int
664e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
665e9dbad6fSeschrock {
666e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
667e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
668e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
669e9dbad6fSeschrock 	    == NULL)
670e9dbad6fSeschrock 		return (-1);
671e9dbad6fSeschrock 
672e9dbad6fSeschrock 	return (0);
673e9dbad6fSeschrock }
674e9dbad6fSeschrock 
675e9dbad6fSeschrock /*
676a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
677e9dbad6fSeschrock  */
678e9dbad6fSeschrock void
679e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
680e9dbad6fSeschrock {
681990b4856Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
682e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
683e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
684e9dbad6fSeschrock }
685e9dbad6fSeschrock 
686990b4856Slling static int
687990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
688990b4856Slling     nvlist_t *nvl)
689e9dbad6fSeschrock {
690e9dbad6fSeschrock 	char *packed;
691e9dbad6fSeschrock 	size_t len;
692e9dbad6fSeschrock 
693e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
694e9dbad6fSeschrock 
695e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
696e9dbad6fSeschrock 		return (-1);
697e9dbad6fSeschrock 
698e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
699e9dbad6fSeschrock 
700990b4856Slling 	*outnv = (uint64_t)(uintptr_t)packed;
701990b4856Slling 	*outlen = len;
702e9dbad6fSeschrock 
703e9dbad6fSeschrock 	return (0);
704e9dbad6fSeschrock }
705e9dbad6fSeschrock 
706990b4856Slling int
707990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
708990b4856Slling {
709990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
710990b4856Slling 	    &zc->zc_nvlist_conf_size, nvl));
711990b4856Slling }
712990b4856Slling 
713990b4856Slling int
714990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
715990b4856Slling {
716990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
717990b4856Slling 	    &zc->zc_nvlist_src_size, nvl));
718990b4856Slling }
719990b4856Slling 
720e9dbad6fSeschrock /*
721e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
722e9dbad6fSeschrock  */
723e9dbad6fSeschrock int
724e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
725e9dbad6fSeschrock {
726e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
727e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
728e9dbad6fSeschrock 		return (no_memory(hdl));
729e9dbad6fSeschrock 
730e9dbad6fSeschrock 	return (0);
731e9dbad6fSeschrock }
732b1b8ab34Slling 
733990b4856Slling int
734990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
735990b4856Slling {
736990b4856Slling 	int error;
737990b4856Slling 
738990b4856Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
739990b4856Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
740990b4856Slling 	if (hdl->libzfs_log_str) {
741990b4856Slling 		free(hdl->libzfs_log_str);
742990b4856Slling 		hdl->libzfs_log_str = NULL;
743990b4856Slling 	}
744990b4856Slling 	zc->zc_history = 0;
745990b4856Slling 
746990b4856Slling 	return (error);
747990b4856Slling }
748990b4856Slling 
749990b4856Slling /*
750990b4856Slling  * ================================================================
751990b4856Slling  * API shared by zfs and zpool property management
752990b4856Slling  * ================================================================
753990b4856Slling  */
754990b4856Slling 
755b1b8ab34Slling static void
756990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
757b1b8ab34Slling {
758990b4856Slling 	zprop_list_t *pl = cbp->cb_proplist;
759b1b8ab34Slling 	int i;
760b1b8ab34Slling 	char *title;
761b1b8ab34Slling 	size_t len;
762b1b8ab34Slling 
763b1b8ab34Slling 	cbp->cb_first = B_FALSE;
764b1b8ab34Slling 	if (cbp->cb_scripted)
765b1b8ab34Slling 		return;
766b1b8ab34Slling 
767b1b8ab34Slling 	/*
768b1b8ab34Slling 	 * Start with the length of the column headers.
769b1b8ab34Slling 	 */
770b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
771b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
772b1b8ab34Slling 	    "PROPERTY"));
773b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
774b1b8ab34Slling 	    "VALUE"));
775b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
776b1b8ab34Slling 	    "SOURCE"));
777b1b8ab34Slling 
778b1b8ab34Slling 	/*
779b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
780b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
781b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
782b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
783b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
784b1b8ab34Slling 	 * if the name of the property is much longer the any values we find.
785b1b8ab34Slling 	 */
786b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
787b1b8ab34Slling 		/*
788b1b8ab34Slling 		 * 'PROPERTY' column
789b1b8ab34Slling 		 */
790990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
791990b4856Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
792990b4856Slling 			    zpool_prop_to_name(pl->pl_prop) :
793990b4856Slling 			    zfs_prop_to_name(pl->pl_prop);
794990b4856Slling 
795990b4856Slling 			len = strlen(propname);
796b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
797b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
798b1b8ab34Slling 		} else {
799b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
800b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
801b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
802b1b8ab34Slling 		}
803b1b8ab34Slling 
804b1b8ab34Slling 		/*
805b1b8ab34Slling 		 * 'VALUE' column
806b1b8ab34Slling 		 */
807b1b8ab34Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
808b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
809b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
810b1b8ab34Slling 
811b1b8ab34Slling 		/*
812b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
813b1b8ab34Slling 		 */
814990b4856Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
815990b4856Slling 		    ZFS_PROP_NAME) &&
816b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
817b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
818b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
819b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
820b1b8ab34Slling 		}
821b1b8ab34Slling 	}
822b1b8ab34Slling 
823b1b8ab34Slling 	/*
824b1b8ab34Slling 	 * Now go through and print the headers.
825b1b8ab34Slling 	 */
826b1b8ab34Slling 	for (i = 0; i < 4; i++) {
827b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
828b1b8ab34Slling 		case GET_COL_NAME:
829b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
830b1b8ab34Slling 			break;
831b1b8ab34Slling 		case GET_COL_PROPERTY:
832b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
833b1b8ab34Slling 			break;
834b1b8ab34Slling 		case GET_COL_VALUE:
835b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
836b1b8ab34Slling 			break;
837b1b8ab34Slling 		case GET_COL_SOURCE:
838b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
839b1b8ab34Slling 			break;
840b1b8ab34Slling 		default:
841b1b8ab34Slling 			title = NULL;
842b1b8ab34Slling 		}
843b1b8ab34Slling 
844b1b8ab34Slling 		if (title != NULL) {
845b1b8ab34Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
846b1b8ab34Slling 				(void) printf("%s", title);
847b1b8ab34Slling 			else
848b1b8ab34Slling 				(void) printf("%-*s  ",
849b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
850b1b8ab34Slling 				    title);
851b1b8ab34Slling 		}
852b1b8ab34Slling 	}
853b1b8ab34Slling 	(void) printf("\n");
854b1b8ab34Slling }
855b1b8ab34Slling 
856b1b8ab34Slling /*
857b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
858b1b8ab34Slling  * structure.
859b1b8ab34Slling  */
860b1b8ab34Slling void
861990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
862990b4856Slling     const char *propname, const char *value, zprop_source_t sourcetype,
863b1b8ab34Slling     const char *source)
864b1b8ab34Slling {
865b1b8ab34Slling 	int i;
866b1b8ab34Slling 	const char *str;
867b1b8ab34Slling 	char buf[128];
868b1b8ab34Slling 
869b1b8ab34Slling 	/*
870b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
871b1b8ab34Slling 	 */
872b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
873b1b8ab34Slling 		return;
874b1b8ab34Slling 
875b1b8ab34Slling 	if (cbp->cb_first)
876990b4856Slling 		zprop_print_headers(cbp, cbp->cb_type);
877b1b8ab34Slling 
878b1b8ab34Slling 	for (i = 0; i < 4; i++) {
879b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
880b1b8ab34Slling 		case GET_COL_NAME:
881b1b8ab34Slling 			str = name;
882b1b8ab34Slling 			break;
883b1b8ab34Slling 
884b1b8ab34Slling 		case GET_COL_PROPERTY:
885b1b8ab34Slling 			str = propname;
886b1b8ab34Slling 			break;
887b1b8ab34Slling 
888b1b8ab34Slling 		case GET_COL_VALUE:
889b1b8ab34Slling 			str = value;
890b1b8ab34Slling 			break;
891b1b8ab34Slling 
892b1b8ab34Slling 		case GET_COL_SOURCE:
893b1b8ab34Slling 			switch (sourcetype) {
894990b4856Slling 			case ZPROP_SRC_NONE:
895b1b8ab34Slling 				str = "-";
896b1b8ab34Slling 				break;
897b1b8ab34Slling 
898990b4856Slling 			case ZPROP_SRC_DEFAULT:
899b1b8ab34Slling 				str = "default";
900b1b8ab34Slling 				break;
901b1b8ab34Slling 
902990b4856Slling 			case ZPROP_SRC_LOCAL:
903b1b8ab34Slling 				str = "local";
904b1b8ab34Slling 				break;
905b1b8ab34Slling 
906990b4856Slling 			case ZPROP_SRC_TEMPORARY:
907b1b8ab34Slling 				str = "temporary";
908b1b8ab34Slling 				break;
909b1b8ab34Slling 
910990b4856Slling 			case ZPROP_SRC_INHERITED:
911b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
912b1b8ab34Slling 				    "inherited from %s", source);
913b1b8ab34Slling 				str = buf;
914b1b8ab34Slling 				break;
915b1b8ab34Slling 			}
916b1b8ab34Slling 			break;
917b1b8ab34Slling 
918b1b8ab34Slling 		default:
919b1b8ab34Slling 			continue;
920b1b8ab34Slling 		}
921b1b8ab34Slling 
922b1b8ab34Slling 		if (cbp->cb_columns[i + 1] == 0)
923b1b8ab34Slling 			(void) printf("%s", str);
924b1b8ab34Slling 		else if (cbp->cb_scripted)
925b1b8ab34Slling 			(void) printf("%s\t", str);
926b1b8ab34Slling 		else
927b1b8ab34Slling 			(void) printf("%-*s  ",
928b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
929b1b8ab34Slling 			    str);
930b1b8ab34Slling 
931b1b8ab34Slling 	}
932b1b8ab34Slling 
933b1b8ab34Slling 	(void) printf("\n");
934b1b8ab34Slling }
935ecd6cf80Smarks 
936990b4856Slling /*
937990b4856Slling  * Given a numeric suffix, convert the value into a number of bits that the
938990b4856Slling  * resulting value must be shifted.
939990b4856Slling  */
940990b4856Slling static int
941990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf)
942990b4856Slling {
943990b4856Slling 	const char *ends = "BKMGTPEZ";
944990b4856Slling 	int i;
945990b4856Slling 
946990b4856Slling 	if (buf[0] == '\0')
947990b4856Slling 		return (0);
948990b4856Slling 	for (i = 0; i < strlen(ends); i++) {
949990b4856Slling 		if (toupper(buf[0]) == ends[i])
950990b4856Slling 			break;
951990b4856Slling 	}
952990b4856Slling 	if (i == strlen(ends)) {
953990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954990b4856Slling 		    "invalid numeric suffix '%s'"), buf);
955990b4856Slling 		return (-1);
956990b4856Slling 	}
957990b4856Slling 
958990b4856Slling 	/*
959990b4856Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
960990b4856Slling 	 * allow 'BB' - that's just weird.
961990b4856Slling 	 */
962990b4856Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
963990b4856Slling 	    toupper(buf[0]) != 'B'))
964990b4856Slling 		return (10*i);
965990b4856Slling 
966990b4856Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
967990b4856Slling 	    "invalid numeric suffix '%s'"), buf);
968990b4856Slling 	return (-1);
969990b4856Slling }
970990b4856Slling 
971990b4856Slling /*
972990b4856Slling  * Convert a string of the form '100G' into a real number.  Used when setting
973990b4856Slling  * properties or creating a volume.  'buf' is used to place an extended error
974990b4856Slling  * message for the caller to use.
975990b4856Slling  */
976ecd6cf80Smarks int
977990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
978ecd6cf80Smarks {
979990b4856Slling 	char *end;
980990b4856Slling 	int shift;
981ecd6cf80Smarks 
982990b4856Slling 	*num = 0;
983990b4856Slling 
984990b4856Slling 	/* Check to see if this looks like a number.  */
985990b4856Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
986990b4856Slling 		if (hdl)
987990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
988990b4856Slling 			    "bad numeric value '%s'"), value);
989990b4856Slling 		return (-1);
990ecd6cf80Smarks 	}
991ecd6cf80Smarks 
992990b4856Slling 	/* Rely on stroll() to process the numeric portion.  */
993990b4856Slling 	errno = 0;
994990b4856Slling 	*num = strtoll(value, &end, 10);
995990b4856Slling 
996990b4856Slling 	/*
997990b4856Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
998990b4856Slling 	 * in a 64-bit value.
999990b4856Slling 	 */
1000990b4856Slling 	if (errno == ERANGE) {
1001990b4856Slling 		if (hdl)
1002990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003990b4856Slling 			    "numeric value is too large"));
1004990b4856Slling 		return (-1);
1005990b4856Slling 	}
1006990b4856Slling 
1007990b4856Slling 	/*
1008990b4856Slling 	 * If we have a decimal value, then do the computation with floating
1009990b4856Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1010990b4856Slling 	 */
1011990b4856Slling 	if (*end == '.') {
1012990b4856Slling 		double fval = strtod(value, &end);
1013990b4856Slling 
1014990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1015990b4856Slling 			return (-1);
1016990b4856Slling 
1017990b4856Slling 		fval *= pow(2, shift);
1018990b4856Slling 
1019990b4856Slling 		if (fval > UINT64_MAX) {
1020990b4856Slling 			if (hdl)
1021990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1022990b4856Slling 				    "numeric value is too large"));
1023990b4856Slling 			return (-1);
1024990b4856Slling 		}
1025990b4856Slling 
1026990b4856Slling 		*num = (uint64_t)fval;
1027990b4856Slling 	} else {
1028990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1029990b4856Slling 			return (-1);
1030990b4856Slling 
1031990b4856Slling 		/* Check for overflow */
1032990b4856Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1033990b4856Slling 			if (hdl)
1034990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1035990b4856Slling 				    "numeric value is too large"));
1036990b4856Slling 			return (-1);
1037990b4856Slling 		}
1038990b4856Slling 
1039990b4856Slling 		*num <<= shift;
1040990b4856Slling 	}
1041990b4856Slling 
1042990b4856Slling 	return (0);
1043990b4856Slling }
1044990b4856Slling 
1045990b4856Slling /*
1046990b4856Slling  * Given a propname=value nvpair to set, parse any numeric properties
1047990b4856Slling  * (index, boolean, etc) if they are specified as strings and add the
1048990b4856Slling  * resulting nvpair to the returned nvlist.
1049990b4856Slling  *
1050990b4856Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1051990b4856Slling  * We want the user to be able to ignore this fact and specify properties
1052990b4856Slling  * as native values (numbers, for example) or as strings (to simplify
1053990b4856Slling  * command line utilities).  This also handles converting index types
1054990b4856Slling  * (compression, checksum, etc) from strings to their on-disk index.
1055990b4856Slling  */
1056990b4856Slling int
1057990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1058990b4856Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1059990b4856Slling     const char *errbuf)
1060990b4856Slling {
1061990b4856Slling 	data_type_t datatype = nvpair_type(elem);
1062990b4856Slling 	zprop_type_t proptype;
1063990b4856Slling 	const char *propname;
1064990b4856Slling 	char *value;
1065990b4856Slling 	boolean_t isnone = B_FALSE;
1066990b4856Slling 
1067990b4856Slling 	if (type == ZFS_TYPE_POOL) {
1068990b4856Slling 		proptype = zpool_prop_get_type(prop);
1069990b4856Slling 		propname = zpool_prop_to_name(prop);
1070990b4856Slling 	} else {
1071990b4856Slling 		proptype = zfs_prop_get_type(prop);
1072990b4856Slling 		propname = zfs_prop_to_name(prop);
1073990b4856Slling 	}
1074990b4856Slling 
1075990b4856Slling 	/*
1076990b4856Slling 	 * Convert any properties to the internal DSL value types.
1077990b4856Slling 	 */
1078990b4856Slling 	*svalp = NULL;
1079990b4856Slling 	*ivalp = 0;
1080990b4856Slling 
1081990b4856Slling 	switch (proptype) {
1082990b4856Slling 	case PROP_TYPE_STRING:
1083990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1084990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1085990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1086990b4856Slling 			goto error;
1087990b4856Slling 		}
1088990b4856Slling 		(void) nvpair_value_string(elem, svalp);
1089990b4856Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1090990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1091990b4856Slling 			    "'%s' is too long"), nvpair_name(elem));
1092990b4856Slling 			goto error;
1093990b4856Slling 		}
1094990b4856Slling 		break;
1095990b4856Slling 
1096990b4856Slling 	case PROP_TYPE_NUMBER:
1097990b4856Slling 		if (datatype == DATA_TYPE_STRING) {
1098990b4856Slling 			(void) nvpair_value_string(elem, &value);
1099990b4856Slling 			if (strcmp(value, "none") == 0) {
1100990b4856Slling 				isnone = B_TRUE;
1101990b4856Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1102990b4856Slling 			    != 0) {
1103990b4856Slling 				goto error;
1104990b4856Slling 			}
1105990b4856Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1106990b4856Slling 			(void) nvpair_value_uint64(elem, ivalp);
1107990b4856Slling 		} else {
1108990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1109990b4856Slling 			    "'%s' must be a number"), nvpair_name(elem));
1110990b4856Slling 			goto error;
1111990b4856Slling 		}
1112990b4856Slling 
1113990b4856Slling 		/*
1114990b4856Slling 		 * Quota special: force 'none' and don't allow 0.
1115990b4856Slling 		 */
1116990b4856Slling 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 &&
1117990b4856Slling 		    !isnone && prop == ZFS_PROP_QUOTA) {
1118990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1119990b4856Slling 			    "use 'none' to disable quota"));
1120990b4856Slling 			goto error;
1121990b4856Slling 		}
1122990b4856Slling 		break;
1123990b4856Slling 
1124990b4856Slling 	case PROP_TYPE_INDEX:
1125990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1126990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1128990b4856Slling 			goto error;
1129990b4856Slling 		}
1130990b4856Slling 
1131990b4856Slling 		(void) nvpair_value_string(elem, &value);
1132990b4856Slling 
1133990b4856Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1134990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1135990b4856Slling 			    "'%s' must be one of '%s'"), propname,
1136990b4856Slling 			    zprop_values(prop, type));
1137990b4856Slling 			goto error;
1138990b4856Slling 		}
1139990b4856Slling 		break;
1140990b4856Slling 
1141990b4856Slling 	default:
1142990b4856Slling 		abort();
1143990b4856Slling 	}
1144990b4856Slling 
1145990b4856Slling 	/*
1146990b4856Slling 	 * Add the result to our return set of properties.
1147990b4856Slling 	 */
1148990b4856Slling 	if (*svalp != NULL) {
1149990b4856Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1150990b4856Slling 			(void) no_memory(hdl);
1151990b4856Slling 			return (-1);
1152990b4856Slling 		}
1153990b4856Slling 	} else {
1154990b4856Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1155990b4856Slling 			(void) no_memory(hdl);
1156990b4856Slling 			return (-1);
1157990b4856Slling 		}
1158990b4856Slling 	}
1159990b4856Slling 
1160990b4856Slling 	return (0);
1161990b4856Slling error:
1162990b4856Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1163990b4856Slling 	return (-1);
1164990b4856Slling }
1165990b4856Slling 
1166990b4856Slling /*
1167990b4856Slling  * Given a comma-separated list of properties, construct a property list
1168990b4856Slling  * containing both user-defined and native properties.  This function will
1169990b4856Slling  * return a NULL list if 'all' is specified, which can later be expanded
1170990b4856Slling  * by zprop_expand_list().
1171990b4856Slling  */
1172990b4856Slling int
1173990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1174990b4856Slling     zfs_type_t type)
1175990b4856Slling {
1176990b4856Slling 	size_t len;
1177990b4856Slling 	char *s, *p;
1178990b4856Slling 	char c;
1179990b4856Slling 	int prop;
1180990b4856Slling 	zprop_list_t *entry;
1181990b4856Slling 	zprop_list_t **last;
1182990b4856Slling 
1183990b4856Slling 	*listp = NULL;
1184990b4856Slling 	last = listp;
1185990b4856Slling 
1186990b4856Slling 	/*
1187990b4856Slling 	 * If 'all' is specified, return a NULL list.
1188990b4856Slling 	 */
1189990b4856Slling 	if (strcmp(props, "all") == 0)
1190990b4856Slling 		return (0);
1191990b4856Slling 
1192990b4856Slling 	/*
1193990b4856Slling 	 * If no props were specified, return an error.
1194990b4856Slling 	 */
1195990b4856Slling 	if (props[0] == '\0') {
1196990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1197990b4856Slling 		    "no properties specified"));
1198990b4856Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1199990b4856Slling 		    "bad property list")));
1200990b4856Slling 	}
1201990b4856Slling 
1202990b4856Slling 	/*
1203990b4856Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1204990b4856Slling 	 * aliases makes this more effort than it's worth.
1205990b4856Slling 	 */
1206990b4856Slling 	s = props;
1207990b4856Slling 	while (*s != '\0') {
1208990b4856Slling 		if ((p = strchr(s, ',')) == NULL) {
1209990b4856Slling 			len = strlen(s);
1210990b4856Slling 			p = s + len;
1211990b4856Slling 		} else {
1212990b4856Slling 			len = p - s;
1213990b4856Slling 		}
1214990b4856Slling 
1215990b4856Slling 		/*
1216990b4856Slling 		 * Check for empty options.
1217990b4856Slling 		 */
1218990b4856Slling 		if (len == 0) {
1219990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1220990b4856Slling 			    "empty property name"));
1221990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1222990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1223990b4856Slling 		}
1224990b4856Slling 
1225990b4856Slling 		/*
1226990b4856Slling 		 * Check all regular property names.
1227990b4856Slling 		 */
1228990b4856Slling 		c = s[len];
1229990b4856Slling 		s[len] = '\0';
1230990b4856Slling 		prop = zprop_name_to_prop(s, type);
1231990b4856Slling 
1232990b4856Slling 		if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1233990b4856Slling 			prop = ZPROP_INVAL;
1234990b4856Slling 
1235990b4856Slling 		/*
1236990b4856Slling 		 * When no property table entry can be found, return failure if
1237990b4856Slling 		 * this is a pool property or if this isn't a user-defined
1238990b4856Slling 		 * dataset property,
1239990b4856Slling 		 */
1240990b4856Slling 		if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1241990b4856Slling 		    !zfs_prop_user(s))) {
1242990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1243990b4856Slling 			    "invalid property '%s'"), s);
1244990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1245990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1246990b4856Slling 		}
1247990b4856Slling 
1248990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1249990b4856Slling 			return (-1);
1250990b4856Slling 
1251990b4856Slling 		entry->pl_prop = prop;
1252990b4856Slling 		if (prop == ZPROP_INVAL) {
1253990b4856Slling 			if ((entry->pl_user_prop = zfs_strdup(hdl, s))
1254990b4856Slling 			    == NULL) {
1255990b4856Slling 				free(entry);
1256990b4856Slling 				return (-1);
1257990b4856Slling 			}
1258990b4856Slling 			entry->pl_width = strlen(s);
1259990b4856Slling 		} else {
1260990b4856Slling 			entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1261990b4856Slling 			    type);
1262990b4856Slling 		}
1263990b4856Slling 
1264990b4856Slling 		*last = entry;
1265990b4856Slling 		last = &entry->pl_next;
1266990b4856Slling 
1267990b4856Slling 		s = p;
1268990b4856Slling 		if (c == ',')
1269990b4856Slling 			s++;
1270990b4856Slling 	}
1271990b4856Slling 
1272990b4856Slling 	return (0);
1273990b4856Slling }
1274990b4856Slling 
1275990b4856Slling void
1276990b4856Slling zprop_free_list(zprop_list_t *pl)
1277990b4856Slling {
1278990b4856Slling 	zprop_list_t *next;
1279990b4856Slling 
1280990b4856Slling 	while (pl != NULL) {
1281990b4856Slling 		next = pl->pl_next;
1282990b4856Slling 		free(pl->pl_user_prop);
1283990b4856Slling 		free(pl);
1284990b4856Slling 		pl = next;
1285990b4856Slling 	}
1286990b4856Slling }
1287990b4856Slling 
1288990b4856Slling typedef struct expand_data {
1289990b4856Slling 	zprop_list_t	**last;
1290990b4856Slling 	libzfs_handle_t	*hdl;
1291990b4856Slling 	zfs_type_t type;
1292990b4856Slling } expand_data_t;
1293990b4856Slling 
1294990b4856Slling int
1295990b4856Slling zprop_expand_list_cb(int prop, void *cb)
1296990b4856Slling {
1297990b4856Slling 	zprop_list_t *entry;
1298990b4856Slling 	expand_data_t *edp = cb;
1299990b4856Slling 
1300990b4856Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1301990b4856Slling 		return (ZPROP_INVAL);
1302990b4856Slling 
1303990b4856Slling 	entry->pl_prop = prop;
1304990b4856Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1305990b4856Slling 	entry->pl_all = B_TRUE;
1306990b4856Slling 
1307990b4856Slling 	*(edp->last) = entry;
1308990b4856Slling 	edp->last = &entry->pl_next;
1309990b4856Slling 
1310990b4856Slling 	return (ZPROP_CONT);
1311990b4856Slling }
1312990b4856Slling 
1313990b4856Slling int
1314990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1315990b4856Slling {
1316990b4856Slling 	zprop_list_t *entry;
1317990b4856Slling 	zprop_list_t **last;
1318990b4856Slling 	expand_data_t exp;
1319990b4856Slling 
1320990b4856Slling 	if (*plp == NULL) {
1321990b4856Slling 		/*
1322990b4856Slling 		 * If this is the very first time we've been called for an 'all'
1323990b4856Slling 		 * specification, expand the list to include all native
1324990b4856Slling 		 * properties.
1325990b4856Slling 		 */
1326990b4856Slling 		last = plp;
1327990b4856Slling 
1328990b4856Slling 		exp.last = last;
1329990b4856Slling 		exp.hdl = hdl;
1330990b4856Slling 		exp.type = type;
1331990b4856Slling 
1332990b4856Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1333990b4856Slling 		    B_FALSE, type) == ZPROP_INVAL)
1334990b4856Slling 			return (-1);
1335990b4856Slling 
1336990b4856Slling 		/*
1337990b4856Slling 		 * Add 'name' to the beginning of the list, which is handled
1338990b4856Slling 		 * specially.
1339990b4856Slling 		 */
1340990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1341990b4856Slling 			return (-1);
1342990b4856Slling 
1343990b4856Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1344990b4856Slling 		    ZFS_PROP_NAME;
1345990b4856Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1346990b4856Slling 		    &entry->pl_fixed, type);
1347990b4856Slling 		entry->pl_all = B_TRUE;
1348990b4856Slling 		entry->pl_next = *plp;
1349990b4856Slling 		*plp = entry;
1350990b4856Slling 	}
1351990b4856Slling 	return (0);
1352990b4856Slling }
1353990b4856Slling 
1354990b4856Slling int
1355990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1356990b4856Slling     zfs_type_t type)
1357990b4856Slling {
1358990b4856Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1359ecd6cf80Smarks }
1360