xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision 43afaaa8b73f73af765f4fa90f39a0f86cb8a364)
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 /*
2297d9e3a6Sck  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * Internal utility routines for the ZFS library.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <errno.h>
31fa9e4066Sahrens #include <fcntl.h>
32fa9e4066Sahrens #include <libintl.h>
33fa9e4066Sahrens #include <stdarg.h>
34fa9e4066Sahrens #include <stdio.h>
35fa9e4066Sahrens #include <stdlib.h>
36fa9e4066Sahrens #include <strings.h>
37fa9e4066Sahrens #include <unistd.h>
38990b4856Slling #include <ctype.h>
39990b4856Slling #include <math.h>
40fa9e4066Sahrens #include <sys/mnttab.h>
415aba80dbSck #include <sys/mntent.h>
425aba80dbSck #include <sys/types.h>
43fa9e4066Sahrens 
44fa9e4066Sahrens #include <libzfs.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens #include "libzfs_impl.h"
4791ebeef5Sahrens #include "zfs_prop.h"
48fa9e4066Sahrens 
4999653d4eSeschrock int
5099653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
5199653d4eSeschrock {
5299653d4eSeschrock 	return (hdl->libzfs_error);
5399653d4eSeschrock }
54fa9e4066Sahrens 
5599653d4eSeschrock const char *
5699653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
5799653d4eSeschrock {
5899653d4eSeschrock 	return (hdl->libzfs_action);
5999653d4eSeschrock }
60fa9e4066Sahrens 
6199653d4eSeschrock const char *
6299653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl)
6399653d4eSeschrock {
6499653d4eSeschrock 	if (hdl->libzfs_desc[0] != '\0')
6599653d4eSeschrock 		return (hdl->libzfs_desc);
6699653d4eSeschrock 
6799653d4eSeschrock 	switch (hdl->libzfs_error) {
6899653d4eSeschrock 	case EZFS_NOMEM:
6999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
7099653d4eSeschrock 	case EZFS_BADPROP:
7199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
7299653d4eSeschrock 	case EZFS_PROPREADONLY:
7399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "read only property"));
7499653d4eSeschrock 	case EZFS_PROPTYPE:
7599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
7699653d4eSeschrock 		    "datasets of this type"));
7799653d4eSeschrock 	case EZFS_PROPNONINHERIT:
7899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
7999653d4eSeschrock 	case EZFS_PROPSPACE:
8099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
8199653d4eSeschrock 	case EZFS_BADTYPE:
8299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
8399653d4eSeschrock 		    "datasets of this type"));
8499653d4eSeschrock 	case EZFS_BUSY:
8599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
8699653d4eSeschrock 	case EZFS_EXISTS:
8799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
8899653d4eSeschrock 	case EZFS_NOENT:
8999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
9099653d4eSeschrock 	case EZFS_BADSTREAM:
9199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
9299653d4eSeschrock 	case EZFS_DSREADONLY:
9399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
9499653d4eSeschrock 	case EZFS_VOLTOOBIG:
9599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
9699653d4eSeschrock 		    "this system"));
9799653d4eSeschrock 	case EZFS_VOLHASDATA:
9899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume has data"));
9999653d4eSeschrock 	case EZFS_INVALIDNAME:
10099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
10199653d4eSeschrock 	case EZFS_BADRESTORE:
10299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
10399653d4eSeschrock 		    "destination"));
10499653d4eSeschrock 	case EZFS_BADBACKUP:
10599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
10699653d4eSeschrock 	case EZFS_BADTARGET:
10799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
10899653d4eSeschrock 	case EZFS_NODEVICE:
10999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
11099653d4eSeschrock 	case EZFS_BADDEV:
11199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
11299653d4eSeschrock 	case EZFS_NOREPLICAS:
11399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
11499653d4eSeschrock 	case EZFS_RESILVERING:
11599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
11699653d4eSeschrock 	case EZFS_BADVERSION:
11799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
11899653d4eSeschrock 	case EZFS_POOLUNAVAIL:
11999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
12099653d4eSeschrock 	case EZFS_DEVOVERFLOW:
12199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
12299653d4eSeschrock 	case EZFS_BADPATH:
12399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
12499653d4eSeschrock 	case EZFS_CROSSTARGET:
12599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
12699653d4eSeschrock 		    "pools"));
12799653d4eSeschrock 	case EZFS_ZONED:
12899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
12999653d4eSeschrock 	case EZFS_MOUNTFAILED:
13099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
13199653d4eSeschrock 	case EZFS_UMOUNTFAILED:
13299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
133f3861e1aSahl 	case EZFS_UNSHARENFSFAILED:
13499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
135f3861e1aSahl 	case EZFS_SHARENFSFAILED:
13699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
137da6c28aaSamw 	case EZFS_UNSHARESMBFAILED:
138da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
139da6c28aaSamw 	case EZFS_SHARESMBFAILED:
140da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
141ecd6cf80Smarks 	case EZFS_ISCSISVCUNAVAIL:
142ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN,
143ecd6cf80Smarks 		    "iscsitgt service need to be enabled by "
144ecd6cf80Smarks 		    "a privileged user"));
14599653d4eSeschrock 	case EZFS_DEVLINKS:
14699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
14799653d4eSeschrock 	case EZFS_PERM:
14899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
14999653d4eSeschrock 	case EZFS_NOSPC:
15099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
15199653d4eSeschrock 	case EZFS_IO:
15299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
15399653d4eSeschrock 	case EZFS_INTR:
15499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
15599653d4eSeschrock 	case EZFS_ISSPARE:
15699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
15799653d4eSeschrock 		    "spare"));
15899653d4eSeschrock 	case EZFS_INVALCONFIG:
15999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1603bb79becSeschrock 	case EZFS_RECURSIVE:
1613bb79becSeschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
16206eeb2adSek 	case EZFS_NOHISTORY:
16306eeb2adSek 		return (dgettext(TEXT_DOMAIN, "no history available"));
164f3861e1aSahl 	case EZFS_UNSHAREISCSIFAILED:
165f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
166f3861e1aSahl 		    "iscsitgtd failed request to unshare"));
167f3861e1aSahl 	case EZFS_SHAREISCSIFAILED:
168f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
169f3861e1aSahl 		    "iscsitgtd failed request to share"));
170b1b8ab34Slling 	case EZFS_POOLPROPS:
171b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
172b1b8ab34Slling 		    "pool properties"));
173b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
174b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
175b1b8ab34Slling 		    "on this type of pool"));
176b1b8ab34Slling 	case EZFS_POOL_INVALARG:
177b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
178b1b8ab34Slling 		    "this pool operation"));
179b7661cccSmmusante 	case EZFS_NAMETOOLONG:
180b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1818488aeb5Staylor 	case EZFS_OPENFAILED:
1828488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1838488aeb5Staylor 	case EZFS_NOCAP:
1848488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1858488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1868488aeb5Staylor 	case EZFS_LABELFAILED:
1878488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
188ecd6cf80Smarks 	case EZFS_BADWHO:
189ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
190ecd6cf80Smarks 	case EZFS_BADPERM:
191ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
192ecd6cf80Smarks 	case EZFS_BADPERMSET:
193ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
194ecd6cf80Smarks 	case EZFS_NODELEGATION:
195ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
196ecd6cf80Smarks 		    "disabled on pool"));
197ecd6cf80Smarks 	case EZFS_PERMRDONLY:
198ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
199ecd6cf80Smarks 		    " modified"));
2002f8aaab3Seschrock 	case EZFS_BADCACHE:
2012f8aaab3Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
202fa94a07fSbrendan 	case EZFS_ISL2CACHE:
203fa94a07fSbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
204e7cbe64fSgw 	case EZFS_VDEVNOTSUP:
205e7cbe64fSgw 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
206e7cbe64fSgw 		    "supported"));
20715e6edf1Sgw 	case EZFS_NOTSUP:
20815e6edf1Sgw 		return (dgettext(TEXT_DOMAIN, "operation not supported "
20915e6edf1Sgw 		    "on this dataset"));
21089a89ebfSlling 	case EZFS_ACTIVE_SPARE:
21189a89ebfSlling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
21289a89ebfSlling 		    "device"));
21399653d4eSeschrock 	case EZFS_UNKNOWN:
21499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
21599653d4eSeschrock 	default:
216c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
217c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
21899653d4eSeschrock 	}
21999653d4eSeschrock }
22099653d4eSeschrock 
22199653d4eSeschrock /*PRINTFLIKE2*/
222fa9e4066Sahrens void
22399653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
224fa9e4066Sahrens {
225fa9e4066Sahrens 	va_list ap;
226fa9e4066Sahrens 
227fa9e4066Sahrens 	va_start(ap, fmt);
228fa9e4066Sahrens 
22999653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
23099653d4eSeschrock 	    fmt, ap);
23199653d4eSeschrock 	hdl->libzfs_desc_active = 1;
23299653d4eSeschrock 
23399653d4eSeschrock 	va_end(ap);
23499653d4eSeschrock }
23599653d4eSeschrock 
23699653d4eSeschrock static void
23799653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
23899653d4eSeschrock {
23999653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
24099653d4eSeschrock 	    fmt, ap);
24199653d4eSeschrock 	hdl->libzfs_error = error;
24299653d4eSeschrock 
24399653d4eSeschrock 	if (hdl->libzfs_desc_active)
24499653d4eSeschrock 		hdl->libzfs_desc_active = 0;
24599653d4eSeschrock 	else
24699653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
24799653d4eSeschrock 
24899653d4eSeschrock 	if (hdl->libzfs_printerr) {
24999653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
25099653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
25199653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
25299653d4eSeschrock 			abort();
25399653d4eSeschrock 		}
25499653d4eSeschrock 
25599653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
256b1b8ab34Slling 		    libzfs_error_description(hdl));
25799653d4eSeschrock 		if (error == EZFS_NOMEM)
25899653d4eSeschrock 			exit(1);
259fa9e4066Sahrens 	}
26099653d4eSeschrock }
26199653d4eSeschrock 
262ece3d9b3Slling int
263ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
264ece3d9b3Slling {
265ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
266ece3d9b3Slling }
267ece3d9b3Slling 
26899653d4eSeschrock /*PRINTFLIKE3*/
26999653d4eSeschrock int
270ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
27199653d4eSeschrock {
27299653d4eSeschrock 	va_list ap;
27399653d4eSeschrock 
27499653d4eSeschrock 	va_start(ap, fmt);
27599653d4eSeschrock 
27699653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
277fa9e4066Sahrens 
278fa9e4066Sahrens 	va_end(ap);
27999653d4eSeschrock 
28099653d4eSeschrock 	return (-1);
281fa9e4066Sahrens }
282fa9e4066Sahrens 
28399653d4eSeschrock static int
28499653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
28599653d4eSeschrock     va_list ap)
28699653d4eSeschrock {
28799653d4eSeschrock 	switch (error) {
28899653d4eSeschrock 	case EPERM:
28999653d4eSeschrock 	case EACCES:
29099653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
29199653d4eSeschrock 		return (-1);
29299653d4eSeschrock 
293ecd6cf80Smarks 	case ECANCELED:
294ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
295ecd6cf80Smarks 		return (-1);
296ecd6cf80Smarks 
29799653d4eSeschrock 	case EIO:
29899653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
29999653d4eSeschrock 		return (-1);
30099653d4eSeschrock 
30199653d4eSeschrock 	case EINTR:
30299653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
30399653d4eSeschrock 		return (-1);
30499653d4eSeschrock 	}
30599653d4eSeschrock 
30699653d4eSeschrock 	return (0);
30799653d4eSeschrock }
30899653d4eSeschrock 
309ece3d9b3Slling int
310ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
311ece3d9b3Slling {
312ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
313ece3d9b3Slling }
314ece3d9b3Slling 
31599653d4eSeschrock /*PRINTFLIKE3*/
31699653d4eSeschrock int
317ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
318fa9e4066Sahrens {
319fa9e4066Sahrens 	va_list ap;
320fa9e4066Sahrens 
321fa9e4066Sahrens 	va_start(ap, fmt);
322fa9e4066Sahrens 
32399653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
32499653d4eSeschrock 		va_end(ap);
32599653d4eSeschrock 		return (-1);
326fa9e4066Sahrens 	}
327fa9e4066Sahrens 
32899653d4eSeschrock 	switch (error) {
32999653d4eSeschrock 	case ENXIO:
33097d9e3a6Sck 	case ENODEV:
33199653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
33299653d4eSeschrock 		break;
33399653d4eSeschrock 
33499653d4eSeschrock 	case ENOENT:
33599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33699653d4eSeschrock 		    "dataset does not exist"));
33799653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
33899653d4eSeschrock 		break;
33999653d4eSeschrock 
34099653d4eSeschrock 	case ENOSPC:
34199653d4eSeschrock 	case EDQUOT:
34299653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
34399653d4eSeschrock 		return (-1);
34499653d4eSeschrock 
34599653d4eSeschrock 	case EEXIST:
34699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34799653d4eSeschrock 		    "dataset already exists"));
34899653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
34999653d4eSeschrock 		break;
35099653d4eSeschrock 
35199653d4eSeschrock 	case EBUSY:
35299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35399653d4eSeschrock 		    "dataset is busy"));
35499653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
35599653d4eSeschrock 		break;
356ecd6cf80Smarks 	case EROFS:
357ecd6cf80Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
358ecd6cf80Smarks 		    "snapshot permissions cannot be modified"));
359ecd6cf80Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
360ecd6cf80Smarks 		break;
361b7661cccSmmusante 	case ENAMETOOLONG:
362b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
363b7661cccSmmusante 		break;
36440ff3960Sck 	case ENOTSUP:
36540ff3960Sck 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
36640ff3960Sck 		break;
36799653d4eSeschrock 	default:
36899653d4eSeschrock 		zfs_error_aux(hdl, strerror(errno));
36999653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
37099653d4eSeschrock 		break;
37199653d4eSeschrock 	}
37299653d4eSeschrock 
37399653d4eSeschrock 	va_end(ap);
37499653d4eSeschrock 	return (-1);
375fa9e4066Sahrens }
376fa9e4066Sahrens 
377ece3d9b3Slling int
378ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
379ece3d9b3Slling {
380ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
381ece3d9b3Slling }
382ece3d9b3Slling 
38399653d4eSeschrock /*PRINTFLIKE3*/
38499653d4eSeschrock int
385ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
386fa9e4066Sahrens {
38799653d4eSeschrock 	va_list ap;
38899653d4eSeschrock 
38999653d4eSeschrock 	va_start(ap, fmt);
39099653d4eSeschrock 
39199653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
39299653d4eSeschrock 		va_end(ap);
39399653d4eSeschrock 		return (-1);
39499653d4eSeschrock 	}
39599653d4eSeschrock 
39699653d4eSeschrock 	switch (error) {
39799653d4eSeschrock 	case ENODEV:
39899653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
39999653d4eSeschrock 		break;
40099653d4eSeschrock 
40199653d4eSeschrock 	case ENOENT:
402b1b8ab34Slling 		zfs_error_aux(hdl,
403b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
40499653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
40599653d4eSeschrock 		break;
40699653d4eSeschrock 
40799653d4eSeschrock 	case EEXIST:
40899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40999653d4eSeschrock 		    "pool already exists"));
41099653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
41199653d4eSeschrock 		break;
41299653d4eSeschrock 
41399653d4eSeschrock 	case EBUSY:
41499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
415*43afaaa8SEric Schrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
41699653d4eSeschrock 		break;
41799653d4eSeschrock 
41899653d4eSeschrock 	case ENXIO:
41999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42099653d4eSeschrock 		    "one or more devices is currently unavailable"));
42199653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
42299653d4eSeschrock 		break;
42399653d4eSeschrock 
42499653d4eSeschrock 	case ENAMETOOLONG:
42599653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
42699653d4eSeschrock 		break;
42799653d4eSeschrock 
428b1b8ab34Slling 	case ENOTSUP:
429b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
430b1b8ab34Slling 		break;
431b1b8ab34Slling 
432b1b8ab34Slling 	case EINVAL:
433b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
434b1b8ab34Slling 		break;
435b1b8ab34Slling 
436ecd6cf80Smarks 	case ENOSPC:
437ecd6cf80Smarks 	case EDQUOT:
438ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
439ecd6cf80Smarks 		return (-1);
440ecd6cf80Smarks 
44199653d4eSeschrock 	default:
44299653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
44399653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
44499653d4eSeschrock 	}
44599653d4eSeschrock 
44699653d4eSeschrock 	va_end(ap);
44799653d4eSeschrock 	return (-1);
448fa9e4066Sahrens }
449fa9e4066Sahrens 
450fa9e4066Sahrens /*
451fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
452fa9e4066Sahrens  */
45399653d4eSeschrock int
45499653d4eSeschrock no_memory(libzfs_handle_t *hdl)
455fa9e4066Sahrens {
45699653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
457fa9e4066Sahrens }
458fa9e4066Sahrens 
459fa9e4066Sahrens /*
460fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
461fa9e4066Sahrens  */
462fa9e4066Sahrens void *
46399653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
464fa9e4066Sahrens {
465fa9e4066Sahrens 	void *data;
466fa9e4066Sahrens 
467fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
46899653d4eSeschrock 		(void) no_memory(hdl);
469fa9e4066Sahrens 
470fa9e4066Sahrens 	return (data);
471fa9e4066Sahrens }
472fa9e4066Sahrens 
473e9dbad6fSeschrock /*
474e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
475e9dbad6fSeschrock  */
476e9dbad6fSeschrock void *
477e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
478e9dbad6fSeschrock {
479e9dbad6fSeschrock 	void *ret;
480e9dbad6fSeschrock 
481e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
482e9dbad6fSeschrock 		(void) no_memory(hdl);
483e9dbad6fSeschrock 		free(ptr);
484e9dbad6fSeschrock 		return (NULL);
485e9dbad6fSeschrock 	}
486e9dbad6fSeschrock 
487e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
488e9dbad6fSeschrock 	return (ret);
489e9dbad6fSeschrock }
490e9dbad6fSeschrock 
491fa9e4066Sahrens /*
492fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
493fa9e4066Sahrens  */
494fa9e4066Sahrens char *
49599653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
496fa9e4066Sahrens {
497fa9e4066Sahrens 	char *ret;
498fa9e4066Sahrens 
499fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
50099653d4eSeschrock 		(void) no_memory(hdl);
501fa9e4066Sahrens 
502fa9e4066Sahrens 	return (ret);
503fa9e4066Sahrens }
504fa9e4066Sahrens 
505fa9e4066Sahrens /*
506fa9e4066Sahrens  * Convert a number to an appropriately human-readable output.
507fa9e4066Sahrens  */
508fa9e4066Sahrens void
509fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
510fa9e4066Sahrens {
511fa9e4066Sahrens 	uint64_t n = num;
512fa9e4066Sahrens 	int index = 0;
513fa9e4066Sahrens 	char u;
514fa9e4066Sahrens 
515fa9e4066Sahrens 	while (n >= 1024) {
5165c709891Seschrock 		n /= 1024;
517fa9e4066Sahrens 		index++;
518fa9e4066Sahrens 	}
519fa9e4066Sahrens 
520fa9e4066Sahrens 	u = " KMGTPE"[index];
521fa9e4066Sahrens 
5225c709891Seschrock 	if (index == 0) {
523fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5245c709891Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5255c709891Seschrock 		/*
5265c709891Seschrock 		 * If this is an even multiple of the base, always display
5275c709891Seschrock 		 * without any decimal precision.
5285c709891Seschrock 		 */
529fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5305c709891Seschrock 	} else {
5315c709891Seschrock 		/*
5325c709891Seschrock 		 * We want to choose a precision that reflects the best choice
5335c709891Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5345c709891Seschrock 		 * we have numbers that are very close to an order of magnitude.
5355c709891Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5365c709891Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5375c709891Seschrock 		 * develop some complex heuristics for this, but it's much
5385c709891Seschrock 		 * easier just to try each combination in turn.
5395c709891Seschrock 		 */
5405c709891Seschrock 		int i;
5415c709891Seschrock 		for (i = 2; i >= 0; i--) {
5423d7072f8Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5433d7072f8Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5445c709891Seschrock 				break;
5455c709891Seschrock 		}
5465c709891Seschrock 	}
547fa9e4066Sahrens }
54899653d4eSeschrock 
54999653d4eSeschrock void
55099653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
55199653d4eSeschrock {
55299653d4eSeschrock 	hdl->libzfs_printerr = printerr;
55399653d4eSeschrock }
55499653d4eSeschrock 
55599653d4eSeschrock libzfs_handle_t *
55699653d4eSeschrock libzfs_init(void)
55799653d4eSeschrock {
55899653d4eSeschrock 	libzfs_handle_t *hdl;
55999653d4eSeschrock 
56099653d4eSeschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
56199653d4eSeschrock 		return (NULL);
56299653d4eSeschrock 	}
56399653d4eSeschrock 
564c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
56599653d4eSeschrock 		free(hdl);
56699653d4eSeschrock 		return (NULL);
56799653d4eSeschrock 	}
56899653d4eSeschrock 
56999653d4eSeschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
57099653d4eSeschrock 		(void) close(hdl->libzfs_fd);
57199653d4eSeschrock 		free(hdl);
57299653d4eSeschrock 		return (NULL);
57399653d4eSeschrock 	}
57499653d4eSeschrock 
57599653d4eSeschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
57699653d4eSeschrock 
57791ebeef5Sahrens 	zfs_prop_init();
578990b4856Slling 	zpool_prop_init();
57991ebeef5Sahrens 
58099653d4eSeschrock 	return (hdl);
58199653d4eSeschrock }
58299653d4eSeschrock 
58399653d4eSeschrock void
58499653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
58599653d4eSeschrock {
58699653d4eSeschrock 	(void) close(hdl->libzfs_fd);
58799653d4eSeschrock 	if (hdl->libzfs_mnttab)
58899653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
58999653d4eSeschrock 	if (hdl->libzfs_sharetab)
59099653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
59167331909Sdougm 	zfs_uninit_libshare(hdl);
592ecd6cf80Smarks 	if (hdl->libzfs_log_str)
593ecd6cf80Smarks 		(void) free(hdl->libzfs_log_str);
59429ab75c9Srm 	zpool_free_handles(hdl);
59599653d4eSeschrock 	namespace_clear(hdl);
59699653d4eSeschrock 	free(hdl);
59799653d4eSeschrock }
59899653d4eSeschrock 
59999653d4eSeschrock libzfs_handle_t *
60099653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
60199653d4eSeschrock {
60299653d4eSeschrock 	return (zhp->zpool_hdl);
60399653d4eSeschrock }
60499653d4eSeschrock 
60599653d4eSeschrock libzfs_handle_t *
60699653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
60799653d4eSeschrock {
60899653d4eSeschrock 	return (zhp->zfs_hdl);
60999653d4eSeschrock }
610e9dbad6fSeschrock 
6115aba80dbSck /*
6125aba80dbSck  * Given a name, determine whether or not it's a valid path
6135aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
6145aba80dbSck  * to match the device number.  If not, treat the path as an
6155aba80dbSck  * fs/vol/snap name.
6165aba80dbSck  */
6175aba80dbSck zfs_handle_t *
6185aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6195aba80dbSck {
6205aba80dbSck 	struct stat64 statbuf;
6215aba80dbSck 	struct extmnttab entry;
6225aba80dbSck 	int ret;
6235aba80dbSck 
6245aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6255aba80dbSck 		/*
6265aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
6275aba80dbSck 		 */
6285aba80dbSck 		return (zfs_open(hdl, path, argtype));
6295aba80dbSck 	}
6305aba80dbSck 
6315aba80dbSck 	if (stat64(path, &statbuf) != 0) {
6325aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6335aba80dbSck 		return (NULL);
6345aba80dbSck 	}
6355aba80dbSck 
6365aba80dbSck 	rewind(hdl->libzfs_mnttab);
6375aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6385aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6395aba80dbSck 		    statbuf.st_dev) {
6405aba80dbSck 			break;
6415aba80dbSck 		}
6425aba80dbSck 	}
6435aba80dbSck 	if (ret != 0) {
6445aba80dbSck 		return (NULL);
6455aba80dbSck 	}
6465aba80dbSck 
6475aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6485aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6495aba80dbSck 		    path);
6505aba80dbSck 		return (NULL);
6515aba80dbSck 	}
6525aba80dbSck 
6535aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6545aba80dbSck }
6555aba80dbSck 
656e9dbad6fSeschrock /*
657e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
658e9dbad6fSeschrock  * an ioctl().
659e9dbad6fSeschrock  */
660e9dbad6fSeschrock int
661e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
662e9dbad6fSeschrock {
663e9dbad6fSeschrock 	if (len == 0)
664a2eea2e1Sahrens 		len = 2048;
665e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
666e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
667e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
668e9dbad6fSeschrock 		return (-1);
669e9dbad6fSeschrock 
670e9dbad6fSeschrock 	return (0);
671e9dbad6fSeschrock }
672e9dbad6fSeschrock 
673e9dbad6fSeschrock /*
674e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
675e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
676e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
677e9dbad6fSeschrock  */
678e9dbad6fSeschrock int
679e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
680e9dbad6fSeschrock {
681e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
682e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
683e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
684e9dbad6fSeschrock 	    == NULL)
685e9dbad6fSeschrock 		return (-1);
686e9dbad6fSeschrock 
687e9dbad6fSeschrock 	return (0);
688e9dbad6fSeschrock }
689e9dbad6fSeschrock 
690e9dbad6fSeschrock /*
691a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
692e9dbad6fSeschrock  */
693e9dbad6fSeschrock void
694e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
695e9dbad6fSeschrock {
696990b4856Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
697e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
698e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
699e9dbad6fSeschrock }
700e9dbad6fSeschrock 
701990b4856Slling static int
702990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
703990b4856Slling     nvlist_t *nvl)
704e9dbad6fSeschrock {
705e9dbad6fSeschrock 	char *packed;
706e9dbad6fSeschrock 	size_t len;
707e9dbad6fSeschrock 
708e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
709e9dbad6fSeschrock 
710e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
711e9dbad6fSeschrock 		return (-1);
712e9dbad6fSeschrock 
713e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
714e9dbad6fSeschrock 
715990b4856Slling 	*outnv = (uint64_t)(uintptr_t)packed;
716990b4856Slling 	*outlen = len;
717e9dbad6fSeschrock 
718e9dbad6fSeschrock 	return (0);
719e9dbad6fSeschrock }
720e9dbad6fSeschrock 
721990b4856Slling int
722990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
723990b4856Slling {
724990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
725990b4856Slling 	    &zc->zc_nvlist_conf_size, nvl));
726990b4856Slling }
727990b4856Slling 
728990b4856Slling int
729990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
730990b4856Slling {
731990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
732990b4856Slling 	    &zc->zc_nvlist_src_size, nvl));
733990b4856Slling }
734990b4856Slling 
735e9dbad6fSeschrock /*
736e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
737e9dbad6fSeschrock  */
738e9dbad6fSeschrock int
739e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
740e9dbad6fSeschrock {
741e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
742e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
743e9dbad6fSeschrock 		return (no_memory(hdl));
744e9dbad6fSeschrock 
745e9dbad6fSeschrock 	return (0);
746e9dbad6fSeschrock }
747b1b8ab34Slling 
748990b4856Slling int
749990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
750990b4856Slling {
751990b4856Slling 	int error;
752990b4856Slling 
753990b4856Slling 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
754990b4856Slling 	error = ioctl(hdl->libzfs_fd, request, zc);
755990b4856Slling 	if (hdl->libzfs_log_str) {
756990b4856Slling 		free(hdl->libzfs_log_str);
757990b4856Slling 		hdl->libzfs_log_str = NULL;
758990b4856Slling 	}
759990b4856Slling 	zc->zc_history = 0;
760990b4856Slling 
761990b4856Slling 	return (error);
762990b4856Slling }
763990b4856Slling 
764990b4856Slling /*
765990b4856Slling  * ================================================================
766990b4856Slling  * API shared by zfs and zpool property management
767990b4856Slling  * ================================================================
768990b4856Slling  */
769990b4856Slling 
770b1b8ab34Slling static void
771990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
772b1b8ab34Slling {
773990b4856Slling 	zprop_list_t *pl = cbp->cb_proplist;
774b1b8ab34Slling 	int i;
775b1b8ab34Slling 	char *title;
776b1b8ab34Slling 	size_t len;
777b1b8ab34Slling 
778b1b8ab34Slling 	cbp->cb_first = B_FALSE;
779b1b8ab34Slling 	if (cbp->cb_scripted)
780b1b8ab34Slling 		return;
781b1b8ab34Slling 
782b1b8ab34Slling 	/*
783b1b8ab34Slling 	 * Start with the length of the column headers.
784b1b8ab34Slling 	 */
785b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
786b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
787b1b8ab34Slling 	    "PROPERTY"));
788b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
789b1b8ab34Slling 	    "VALUE"));
790b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
791b1b8ab34Slling 	    "SOURCE"));
792b1b8ab34Slling 
793b1b8ab34Slling 	/*
794b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
795b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
796b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
797b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
798b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
799b1b8ab34Slling 	 * if the name of the property is much longer the any values we find.
800b1b8ab34Slling 	 */
801b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
802b1b8ab34Slling 		/*
803b1b8ab34Slling 		 * 'PROPERTY' column
804b1b8ab34Slling 		 */
805990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
806990b4856Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
807990b4856Slling 			    zpool_prop_to_name(pl->pl_prop) :
808990b4856Slling 			    zfs_prop_to_name(pl->pl_prop);
809990b4856Slling 
810990b4856Slling 			len = strlen(propname);
811b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
812b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
813b1b8ab34Slling 		} else {
814b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
815b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
816b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
817b1b8ab34Slling 		}
818b1b8ab34Slling 
819b1b8ab34Slling 		/*
820b1b8ab34Slling 		 * 'VALUE' column
821b1b8ab34Slling 		 */
822b1b8ab34Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
823b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
824b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
825b1b8ab34Slling 
826b1b8ab34Slling 		/*
827b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
828b1b8ab34Slling 		 */
829990b4856Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
830990b4856Slling 		    ZFS_PROP_NAME) &&
831b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
832b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
833b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
834b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
835b1b8ab34Slling 		}
836b1b8ab34Slling 	}
837b1b8ab34Slling 
838b1b8ab34Slling 	/*
839b1b8ab34Slling 	 * Now go through and print the headers.
840b1b8ab34Slling 	 */
841b1b8ab34Slling 	for (i = 0; i < 4; i++) {
842b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
843b1b8ab34Slling 		case GET_COL_NAME:
844b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
845b1b8ab34Slling 			break;
846b1b8ab34Slling 		case GET_COL_PROPERTY:
847b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
848b1b8ab34Slling 			break;
849b1b8ab34Slling 		case GET_COL_VALUE:
850b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
851b1b8ab34Slling 			break;
852b1b8ab34Slling 		case GET_COL_SOURCE:
853b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
854b1b8ab34Slling 			break;
855b1b8ab34Slling 		default:
856b1b8ab34Slling 			title = NULL;
857b1b8ab34Slling 		}
858b1b8ab34Slling 
859b1b8ab34Slling 		if (title != NULL) {
860b1b8ab34Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
861b1b8ab34Slling 				(void) printf("%s", title);
862b1b8ab34Slling 			else
863b1b8ab34Slling 				(void) printf("%-*s  ",
864b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
865b1b8ab34Slling 				    title);
866b1b8ab34Slling 		}
867b1b8ab34Slling 	}
868b1b8ab34Slling 	(void) printf("\n");
869b1b8ab34Slling }
870b1b8ab34Slling 
871b1b8ab34Slling /*
872b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
873b1b8ab34Slling  * structure.
874b1b8ab34Slling  */
875b1b8ab34Slling void
876990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
877990b4856Slling     const char *propname, const char *value, zprop_source_t sourcetype,
878b1b8ab34Slling     const char *source)
879b1b8ab34Slling {
880b1b8ab34Slling 	int i;
881b1b8ab34Slling 	const char *str;
882b1b8ab34Slling 	char buf[128];
883b1b8ab34Slling 
884b1b8ab34Slling 	/*
885b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
886b1b8ab34Slling 	 */
887b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
888b1b8ab34Slling 		return;
889b1b8ab34Slling 
890b1b8ab34Slling 	if (cbp->cb_first)
891990b4856Slling 		zprop_print_headers(cbp, cbp->cb_type);
892b1b8ab34Slling 
893b1b8ab34Slling 	for (i = 0; i < 4; i++) {
894b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
895b1b8ab34Slling 		case GET_COL_NAME:
896b1b8ab34Slling 			str = name;
897b1b8ab34Slling 			break;
898b1b8ab34Slling 
899b1b8ab34Slling 		case GET_COL_PROPERTY:
900b1b8ab34Slling 			str = propname;
901b1b8ab34Slling 			break;
902b1b8ab34Slling 
903b1b8ab34Slling 		case GET_COL_VALUE:
904b1b8ab34Slling 			str = value;
905b1b8ab34Slling 			break;
906b1b8ab34Slling 
907b1b8ab34Slling 		case GET_COL_SOURCE:
908b1b8ab34Slling 			switch (sourcetype) {
909990b4856Slling 			case ZPROP_SRC_NONE:
910b1b8ab34Slling 				str = "-";
911b1b8ab34Slling 				break;
912b1b8ab34Slling 
913990b4856Slling 			case ZPROP_SRC_DEFAULT:
914b1b8ab34Slling 				str = "default";
915b1b8ab34Slling 				break;
916b1b8ab34Slling 
917990b4856Slling 			case ZPROP_SRC_LOCAL:
918b1b8ab34Slling 				str = "local";
919b1b8ab34Slling 				break;
920b1b8ab34Slling 
921990b4856Slling 			case ZPROP_SRC_TEMPORARY:
922b1b8ab34Slling 				str = "temporary";
923b1b8ab34Slling 				break;
924b1b8ab34Slling 
925990b4856Slling 			case ZPROP_SRC_INHERITED:
926b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
927b1b8ab34Slling 				    "inherited from %s", source);
928b1b8ab34Slling 				str = buf;
929b1b8ab34Slling 				break;
930b1b8ab34Slling 			}
931b1b8ab34Slling 			break;
932b1b8ab34Slling 
933b1b8ab34Slling 		default:
934b1b8ab34Slling 			continue;
935b1b8ab34Slling 		}
936b1b8ab34Slling 
937b1b8ab34Slling 		if (cbp->cb_columns[i + 1] == 0)
938b1b8ab34Slling 			(void) printf("%s", str);
939b1b8ab34Slling 		else if (cbp->cb_scripted)
940b1b8ab34Slling 			(void) printf("%s\t", str);
941b1b8ab34Slling 		else
942b1b8ab34Slling 			(void) printf("%-*s  ",
943b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
944b1b8ab34Slling 			    str);
945b1b8ab34Slling 
946b1b8ab34Slling 	}
947b1b8ab34Slling 
948b1b8ab34Slling 	(void) printf("\n");
949b1b8ab34Slling }
950ecd6cf80Smarks 
951990b4856Slling /*
952990b4856Slling  * Given a numeric suffix, convert the value into a number of bits that the
953990b4856Slling  * resulting value must be shifted.
954990b4856Slling  */
955990b4856Slling static int
956990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf)
957990b4856Slling {
958990b4856Slling 	const char *ends = "BKMGTPEZ";
959990b4856Slling 	int i;
960990b4856Slling 
961990b4856Slling 	if (buf[0] == '\0')
962990b4856Slling 		return (0);
963990b4856Slling 	for (i = 0; i < strlen(ends); i++) {
964990b4856Slling 		if (toupper(buf[0]) == ends[i])
965990b4856Slling 			break;
966990b4856Slling 	}
967990b4856Slling 	if (i == strlen(ends)) {
968990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
969990b4856Slling 		    "invalid numeric suffix '%s'"), buf);
970990b4856Slling 		return (-1);
971990b4856Slling 	}
972990b4856Slling 
973990b4856Slling 	/*
974990b4856Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
975990b4856Slling 	 * allow 'BB' - that's just weird.
976990b4856Slling 	 */
977990b4856Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
978990b4856Slling 	    toupper(buf[0]) != 'B'))
979990b4856Slling 		return (10*i);
980990b4856Slling 
981990b4856Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
982990b4856Slling 	    "invalid numeric suffix '%s'"), buf);
983990b4856Slling 	return (-1);
984990b4856Slling }
985990b4856Slling 
986990b4856Slling /*
987990b4856Slling  * Convert a string of the form '100G' into a real number.  Used when setting
988990b4856Slling  * properties or creating a volume.  'buf' is used to place an extended error
989990b4856Slling  * message for the caller to use.
990990b4856Slling  */
991ecd6cf80Smarks int
992990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
993ecd6cf80Smarks {
994990b4856Slling 	char *end;
995990b4856Slling 	int shift;
996ecd6cf80Smarks 
997990b4856Slling 	*num = 0;
998990b4856Slling 
999990b4856Slling 	/* Check to see if this looks like a number.  */
1000990b4856Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1001990b4856Slling 		if (hdl)
1002990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003990b4856Slling 			    "bad numeric value '%s'"), value);
1004990b4856Slling 		return (-1);
1005ecd6cf80Smarks 	}
1006ecd6cf80Smarks 
1007990b4856Slling 	/* Rely on stroll() to process the numeric portion.  */
1008990b4856Slling 	errno = 0;
1009990b4856Slling 	*num = strtoll(value, &end, 10);
1010990b4856Slling 
1011990b4856Slling 	/*
1012990b4856Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
1013990b4856Slling 	 * in a 64-bit value.
1014990b4856Slling 	 */
1015990b4856Slling 	if (errno == ERANGE) {
1016990b4856Slling 		if (hdl)
1017990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018990b4856Slling 			    "numeric value is too large"));
1019990b4856Slling 		return (-1);
1020990b4856Slling 	}
1021990b4856Slling 
1022990b4856Slling 	/*
1023990b4856Slling 	 * If we have a decimal value, then do the computation with floating
1024990b4856Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1025990b4856Slling 	 */
1026990b4856Slling 	if (*end == '.') {
1027990b4856Slling 		double fval = strtod(value, &end);
1028990b4856Slling 
1029990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1030990b4856Slling 			return (-1);
1031990b4856Slling 
1032990b4856Slling 		fval *= pow(2, shift);
1033990b4856Slling 
1034990b4856Slling 		if (fval > UINT64_MAX) {
1035990b4856Slling 			if (hdl)
1036990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1037990b4856Slling 				    "numeric value is too large"));
1038990b4856Slling 			return (-1);
1039990b4856Slling 		}
1040990b4856Slling 
1041990b4856Slling 		*num = (uint64_t)fval;
1042990b4856Slling 	} else {
1043990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1044990b4856Slling 			return (-1);
1045990b4856Slling 
1046990b4856Slling 		/* Check for overflow */
1047990b4856Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1048990b4856Slling 			if (hdl)
1049990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1050990b4856Slling 				    "numeric value is too large"));
1051990b4856Slling 			return (-1);
1052990b4856Slling 		}
1053990b4856Slling 
1054990b4856Slling 		*num <<= shift;
1055990b4856Slling 	}
1056990b4856Slling 
1057990b4856Slling 	return (0);
1058990b4856Slling }
1059990b4856Slling 
1060990b4856Slling /*
1061990b4856Slling  * Given a propname=value nvpair to set, parse any numeric properties
1062990b4856Slling  * (index, boolean, etc) if they are specified as strings and add the
1063990b4856Slling  * resulting nvpair to the returned nvlist.
1064990b4856Slling  *
1065990b4856Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1066990b4856Slling  * We want the user to be able to ignore this fact and specify properties
1067990b4856Slling  * as native values (numbers, for example) or as strings (to simplify
1068990b4856Slling  * command line utilities).  This also handles converting index types
1069990b4856Slling  * (compression, checksum, etc) from strings to their on-disk index.
1070990b4856Slling  */
1071990b4856Slling int
1072990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1073990b4856Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1074990b4856Slling     const char *errbuf)
1075990b4856Slling {
1076990b4856Slling 	data_type_t datatype = nvpair_type(elem);
1077990b4856Slling 	zprop_type_t proptype;
1078990b4856Slling 	const char *propname;
1079990b4856Slling 	char *value;
1080990b4856Slling 	boolean_t isnone = B_FALSE;
1081990b4856Slling 
1082990b4856Slling 	if (type == ZFS_TYPE_POOL) {
1083990b4856Slling 		proptype = zpool_prop_get_type(prop);
1084990b4856Slling 		propname = zpool_prop_to_name(prop);
1085990b4856Slling 	} else {
1086990b4856Slling 		proptype = zfs_prop_get_type(prop);
1087990b4856Slling 		propname = zfs_prop_to_name(prop);
1088990b4856Slling 	}
1089990b4856Slling 
1090990b4856Slling 	/*
1091990b4856Slling 	 * Convert any properties to the internal DSL value types.
1092990b4856Slling 	 */
1093990b4856Slling 	*svalp = NULL;
1094990b4856Slling 	*ivalp = 0;
1095990b4856Slling 
1096990b4856Slling 	switch (proptype) {
1097990b4856Slling 	case PROP_TYPE_STRING:
1098990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1099990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1100990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1101990b4856Slling 			goto error;
1102990b4856Slling 		}
1103990b4856Slling 		(void) nvpair_value_string(elem, svalp);
1104990b4856Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1105990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1106990b4856Slling 			    "'%s' is too long"), nvpair_name(elem));
1107990b4856Slling 			goto error;
1108990b4856Slling 		}
1109990b4856Slling 		break;
1110990b4856Slling 
1111990b4856Slling 	case PROP_TYPE_NUMBER:
1112990b4856Slling 		if (datatype == DATA_TYPE_STRING) {
1113990b4856Slling 			(void) nvpair_value_string(elem, &value);
1114990b4856Slling 			if (strcmp(value, "none") == 0) {
1115990b4856Slling 				isnone = B_TRUE;
1116990b4856Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1117990b4856Slling 			    != 0) {
1118990b4856Slling 				goto error;
1119990b4856Slling 			}
1120990b4856Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1121990b4856Slling 			(void) nvpair_value_uint64(elem, ivalp);
1122990b4856Slling 		} else {
1123990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1124990b4856Slling 			    "'%s' must be a number"), nvpair_name(elem));
1125990b4856Slling 			goto error;
1126990b4856Slling 		}
1127990b4856Slling 
1128990b4856Slling 		/*
1129990b4856Slling 		 * Quota special: force 'none' and don't allow 0.
1130990b4856Slling 		 */
1131a9799022Sck 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1132a9799022Sck 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1133990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1134a9799022Sck 			    "use 'none' to disable quota/refquota"));
1135990b4856Slling 			goto error;
1136990b4856Slling 		}
1137990b4856Slling 		break;
1138990b4856Slling 
1139990b4856Slling 	case PROP_TYPE_INDEX:
1140a9799022Sck 		if (datatype != DATA_TYPE_STRING) {
1141990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1142990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1143990b4856Slling 			goto error;
1144990b4856Slling 		}
1145990b4856Slling 
1146a9799022Sck 		(void) nvpair_value_string(elem, &value);
1147a9799022Sck 
1148990b4856Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1149990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1150990b4856Slling 			    "'%s' must be one of '%s'"), propname,
1151990b4856Slling 			    zprop_values(prop, type));
1152990b4856Slling 			goto error;
1153990b4856Slling 		}
1154990b4856Slling 		break;
1155990b4856Slling 
1156990b4856Slling 	default:
1157990b4856Slling 		abort();
1158990b4856Slling 	}
1159990b4856Slling 
1160990b4856Slling 	/*
1161990b4856Slling 	 * Add the result to our return set of properties.
1162990b4856Slling 	 */
1163990b4856Slling 	if (*svalp != NULL) {
1164990b4856Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1165990b4856Slling 			(void) no_memory(hdl);
1166990b4856Slling 			return (-1);
1167990b4856Slling 		}
1168990b4856Slling 	} else {
1169990b4856Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1170990b4856Slling 			(void) no_memory(hdl);
1171990b4856Slling 			return (-1);
1172990b4856Slling 		}
1173990b4856Slling 	}
1174990b4856Slling 
1175990b4856Slling 	return (0);
1176990b4856Slling error:
1177990b4856Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1178990b4856Slling 	return (-1);
1179990b4856Slling }
1180990b4856Slling 
1181990b4856Slling /*
1182990b4856Slling  * Given a comma-separated list of properties, construct a property list
1183990b4856Slling  * containing both user-defined and native properties.  This function will
1184990b4856Slling  * return a NULL list if 'all' is specified, which can later be expanded
1185990b4856Slling  * by zprop_expand_list().
1186990b4856Slling  */
1187990b4856Slling int
1188990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1189990b4856Slling     zfs_type_t type)
1190990b4856Slling {
1191990b4856Slling 	size_t len;
1192990b4856Slling 	char *s, *p;
1193990b4856Slling 	char c;
1194990b4856Slling 	int prop;
1195990b4856Slling 	zprop_list_t *entry;
1196990b4856Slling 	zprop_list_t **last;
1197990b4856Slling 
1198990b4856Slling 	*listp = NULL;
1199990b4856Slling 	last = listp;
1200990b4856Slling 
1201990b4856Slling 	/*
1202990b4856Slling 	 * If 'all' is specified, return a NULL list.
1203990b4856Slling 	 */
1204990b4856Slling 	if (strcmp(props, "all") == 0)
1205990b4856Slling 		return (0);
1206990b4856Slling 
1207990b4856Slling 	/*
1208990b4856Slling 	 * If no props were specified, return an error.
1209990b4856Slling 	 */
1210990b4856Slling 	if (props[0] == '\0') {
1211990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1212990b4856Slling 		    "no properties specified"));
1213990b4856Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1214990b4856Slling 		    "bad property list")));
1215990b4856Slling 	}
1216990b4856Slling 
1217990b4856Slling 	/*
1218990b4856Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1219990b4856Slling 	 * aliases makes this more effort than it's worth.
1220990b4856Slling 	 */
1221990b4856Slling 	s = props;
1222990b4856Slling 	while (*s != '\0') {
1223990b4856Slling 		if ((p = strchr(s, ',')) == NULL) {
1224990b4856Slling 			len = strlen(s);
1225990b4856Slling 			p = s + len;
1226990b4856Slling 		} else {
1227990b4856Slling 			len = p - s;
1228990b4856Slling 		}
1229990b4856Slling 
1230990b4856Slling 		/*
1231990b4856Slling 		 * Check for empty options.
1232990b4856Slling 		 */
1233990b4856Slling 		if (len == 0) {
1234990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235990b4856Slling 			    "empty property name"));
1236990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1237990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1238990b4856Slling 		}
1239990b4856Slling 
1240990b4856Slling 		/*
1241990b4856Slling 		 * Check all regular property names.
1242990b4856Slling 		 */
1243990b4856Slling 		c = s[len];
1244990b4856Slling 		s[len] = '\0';
1245990b4856Slling 		prop = zprop_name_to_prop(s, type);
1246990b4856Slling 
1247990b4856Slling 		if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
1248990b4856Slling 			prop = ZPROP_INVAL;
1249990b4856Slling 
1250990b4856Slling 		/*
1251990b4856Slling 		 * When no property table entry can be found, return failure if
1252990b4856Slling 		 * this is a pool property or if this isn't a user-defined
1253990b4856Slling 		 * dataset property,
1254990b4856Slling 		 */
1255990b4856Slling 		if (prop == ZPROP_INVAL && (type == ZFS_TYPE_POOL ||
1256990b4856Slling 		    !zfs_prop_user(s))) {
1257990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1258990b4856Slling 			    "invalid property '%s'"), s);
1259990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1260990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1261990b4856Slling 		}
1262990b4856Slling 
1263990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1264990b4856Slling 			return (-1);
1265990b4856Slling 
1266990b4856Slling 		entry->pl_prop = prop;
1267990b4856Slling 		if (prop == ZPROP_INVAL) {
1268990b4856Slling 			if ((entry->pl_user_prop = zfs_strdup(hdl, s))
1269990b4856Slling 			    == NULL) {
1270990b4856Slling 				free(entry);
1271990b4856Slling 				return (-1);
1272990b4856Slling 			}
1273990b4856Slling 			entry->pl_width = strlen(s);
1274990b4856Slling 		} else {
1275990b4856Slling 			entry->pl_width = zprop_width(prop, &entry->pl_fixed,
1276990b4856Slling 			    type);
1277990b4856Slling 		}
1278990b4856Slling 
1279990b4856Slling 		*last = entry;
1280990b4856Slling 		last = &entry->pl_next;
1281990b4856Slling 
1282990b4856Slling 		s = p;
1283990b4856Slling 		if (c == ',')
1284990b4856Slling 			s++;
1285990b4856Slling 	}
1286990b4856Slling 
1287990b4856Slling 	return (0);
1288990b4856Slling }
1289990b4856Slling 
1290990b4856Slling void
1291990b4856Slling zprop_free_list(zprop_list_t *pl)
1292990b4856Slling {
1293990b4856Slling 	zprop_list_t *next;
1294990b4856Slling 
1295990b4856Slling 	while (pl != NULL) {
1296990b4856Slling 		next = pl->pl_next;
1297990b4856Slling 		free(pl->pl_user_prop);
1298990b4856Slling 		free(pl);
1299990b4856Slling 		pl = next;
1300990b4856Slling 	}
1301990b4856Slling }
1302990b4856Slling 
1303990b4856Slling typedef struct expand_data {
1304990b4856Slling 	zprop_list_t	**last;
1305990b4856Slling 	libzfs_handle_t	*hdl;
1306990b4856Slling 	zfs_type_t type;
1307990b4856Slling } expand_data_t;
1308990b4856Slling 
1309990b4856Slling int
1310990b4856Slling zprop_expand_list_cb(int prop, void *cb)
1311990b4856Slling {
1312990b4856Slling 	zprop_list_t *entry;
1313990b4856Slling 	expand_data_t *edp = cb;
1314990b4856Slling 
1315990b4856Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1316990b4856Slling 		return (ZPROP_INVAL);
1317990b4856Slling 
1318990b4856Slling 	entry->pl_prop = prop;
1319990b4856Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1320990b4856Slling 	entry->pl_all = B_TRUE;
1321990b4856Slling 
1322990b4856Slling 	*(edp->last) = entry;
1323990b4856Slling 	edp->last = &entry->pl_next;
1324990b4856Slling 
1325990b4856Slling 	return (ZPROP_CONT);
1326990b4856Slling }
1327990b4856Slling 
1328990b4856Slling int
1329990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1330990b4856Slling {
1331990b4856Slling 	zprop_list_t *entry;
1332990b4856Slling 	zprop_list_t **last;
1333990b4856Slling 	expand_data_t exp;
1334990b4856Slling 
1335990b4856Slling 	if (*plp == NULL) {
1336990b4856Slling 		/*
1337990b4856Slling 		 * If this is the very first time we've been called for an 'all'
1338990b4856Slling 		 * specification, expand the list to include all native
1339990b4856Slling 		 * properties.
1340990b4856Slling 		 */
1341990b4856Slling 		last = plp;
1342990b4856Slling 
1343990b4856Slling 		exp.last = last;
1344990b4856Slling 		exp.hdl = hdl;
1345990b4856Slling 		exp.type = type;
1346990b4856Slling 
1347990b4856Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1348990b4856Slling 		    B_FALSE, type) == ZPROP_INVAL)
1349990b4856Slling 			return (-1);
1350990b4856Slling 
1351990b4856Slling 		/*
1352990b4856Slling 		 * Add 'name' to the beginning of the list, which is handled
1353990b4856Slling 		 * specially.
1354990b4856Slling 		 */
1355990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1356990b4856Slling 			return (-1);
1357990b4856Slling 
1358990b4856Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1359990b4856Slling 		    ZFS_PROP_NAME;
1360990b4856Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1361990b4856Slling 		    &entry->pl_fixed, type);
1362990b4856Slling 		entry->pl_all = B_TRUE;
1363990b4856Slling 		entry->pl_next = *plp;
1364990b4856Slling 		*plp = entry;
1365990b4856Slling 	}
1366990b4856Slling 	return (0);
1367990b4856Slling }
1368990b4856Slling 
1369990b4856Slling int
1370990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1371990b4856Slling     zfs_type_t type)
1372990b4856Slling {
1373990b4856Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1374ecd6cf80Smarks }
1375