xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision ecd6cf800b63704be73fb264c3f5b6e0dafc068d)
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>
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"
47fa9e4066Sahrens 
4899653d4eSeschrock int
4999653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
5099653d4eSeschrock {
5199653d4eSeschrock 	return (hdl->libzfs_error);
5299653d4eSeschrock }
53fa9e4066Sahrens 
5499653d4eSeschrock const char *
5599653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
5699653d4eSeschrock {
5799653d4eSeschrock 	return (hdl->libzfs_action);
5899653d4eSeschrock }
59fa9e4066Sahrens 
6099653d4eSeschrock const char *
6199653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl)
6299653d4eSeschrock {
6399653d4eSeschrock 	if (hdl->libzfs_desc[0] != '\0')
6499653d4eSeschrock 		return (hdl->libzfs_desc);
6599653d4eSeschrock 
6699653d4eSeschrock 	switch (hdl->libzfs_error) {
6799653d4eSeschrock 	case EZFS_NOMEM:
6899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
6999653d4eSeschrock 	case EZFS_BADPROP:
7099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
7199653d4eSeschrock 	case EZFS_PROPREADONLY:
7299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "read only property"));
7399653d4eSeschrock 	case EZFS_PROPTYPE:
7499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
7599653d4eSeschrock 		    "datasets of this type"));
7699653d4eSeschrock 	case EZFS_PROPNONINHERIT:
7799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
7899653d4eSeschrock 	case EZFS_PROPSPACE:
7999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
8099653d4eSeschrock 	case EZFS_BADTYPE:
8199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
8299653d4eSeschrock 		    "datasets of this type"));
8399653d4eSeschrock 	case EZFS_BUSY:
8499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
8599653d4eSeschrock 	case EZFS_EXISTS:
8699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
8799653d4eSeschrock 	case EZFS_NOENT:
8899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
8999653d4eSeschrock 	case EZFS_BADSTREAM:
9099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
9199653d4eSeschrock 	case EZFS_DSREADONLY:
9299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset is read only"));
9399653d4eSeschrock 	case EZFS_VOLTOOBIG:
9499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
9599653d4eSeschrock 		    "this system"));
9699653d4eSeschrock 	case EZFS_VOLHASDATA:
9799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume has data"));
9899653d4eSeschrock 	case EZFS_INVALIDNAME:
9999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
10099653d4eSeschrock 	case EZFS_BADRESTORE:
10199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
10299653d4eSeschrock 		    "destination"));
10399653d4eSeschrock 	case EZFS_BADBACKUP:
10499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
10599653d4eSeschrock 	case EZFS_BADTARGET:
10699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
10799653d4eSeschrock 	case EZFS_NODEVICE:
10899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
10999653d4eSeschrock 	case EZFS_BADDEV:
11099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
11199653d4eSeschrock 	case EZFS_NOREPLICAS:
11299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
11399653d4eSeschrock 	case EZFS_RESILVERING:
11499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
11599653d4eSeschrock 	case EZFS_BADVERSION:
11699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unsupported version"));
11799653d4eSeschrock 	case EZFS_POOLUNAVAIL:
11899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
11999653d4eSeschrock 	case EZFS_DEVOVERFLOW:
12099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
12199653d4eSeschrock 	case EZFS_BADPATH:
12299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
12399653d4eSeschrock 	case EZFS_CROSSTARGET:
12499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
12599653d4eSeschrock 		    "pools"));
12699653d4eSeschrock 	case EZFS_ZONED:
12799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
12899653d4eSeschrock 	case EZFS_MOUNTFAILED:
12999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
13099653d4eSeschrock 	case EZFS_UMOUNTFAILED:
13199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
132f3861e1aSahl 	case EZFS_UNSHARENFSFAILED:
13399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
134f3861e1aSahl 	case EZFS_SHARENFSFAILED:
13599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
136*ecd6cf80Smarks 	case EZFS_ISCSISVCUNAVAIL:
137*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN,
138*ecd6cf80Smarks 		    "iscsitgt service need to be enabled by "
139*ecd6cf80Smarks 		    "a privileged user"));
14099653d4eSeschrock 	case EZFS_DEVLINKS:
14199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "failed to create /dev links"));
14299653d4eSeschrock 	case EZFS_PERM:
14399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
14499653d4eSeschrock 	case EZFS_NOSPC:
14599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
14699653d4eSeschrock 	case EZFS_IO:
14799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
14899653d4eSeschrock 	case EZFS_INTR:
14999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
15099653d4eSeschrock 	case EZFS_ISSPARE:
15199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
15299653d4eSeschrock 		    "spare"));
15399653d4eSeschrock 	case EZFS_INVALCONFIG:
15499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1553bb79becSeschrock 	case EZFS_RECURSIVE:
1563bb79becSeschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
15706eeb2adSek 	case EZFS_NOHISTORY:
15806eeb2adSek 		return (dgettext(TEXT_DOMAIN, "no history available"));
159f3861e1aSahl 	case EZFS_UNSHAREISCSIFAILED:
160f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
161f3861e1aSahl 		    "iscsitgtd failed request to unshare"));
162f3861e1aSahl 	case EZFS_SHAREISCSIFAILED:
163f3861e1aSahl 		return (dgettext(TEXT_DOMAIN,
164f3861e1aSahl 		    "iscsitgtd failed request to share"));
165b1b8ab34Slling 	case EZFS_POOLPROPS:
166b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
167b1b8ab34Slling 		    "pool properties"));
168b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
169b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
170b1b8ab34Slling 		    "on this type of pool"));
171b1b8ab34Slling 	case EZFS_POOL_INVALARG:
172b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
173b1b8ab34Slling 		    "this pool operation"));
174b7661cccSmmusante 	case EZFS_NAMETOOLONG:
175b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1768488aeb5Staylor 	case EZFS_OPENFAILED:
1778488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1788488aeb5Staylor 	case EZFS_NOCAP:
1798488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1808488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1818488aeb5Staylor 	case EZFS_LABELFAILED:
1828488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
183*ecd6cf80Smarks 	case EZFS_BADWHO:
184*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
185*ecd6cf80Smarks 	case EZFS_BADPERM:
186*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
187*ecd6cf80Smarks 	case EZFS_BADPERMSET:
188*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
189*ecd6cf80Smarks 	case EZFS_PERMSET_CIRCULAR:
190*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN,
191*ecd6cf80Smarks 		    "Cannot define a permission set in terms of itself"));
192*ecd6cf80Smarks 	case EZFS_NODELEGATION:
193*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
194*ecd6cf80Smarks 		    "disabled on pool"));
195*ecd6cf80Smarks 	case EZFS_PERMRDONLY:
196*ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "snapshot permissions cannot be"
197*ecd6cf80Smarks 		    " modified"));
19899653d4eSeschrock 	case EZFS_UNKNOWN:
19999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
20099653d4eSeschrock 	default:
201c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
202c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
20399653d4eSeschrock 	}
20499653d4eSeschrock }
20599653d4eSeschrock 
20699653d4eSeschrock /*PRINTFLIKE2*/
207fa9e4066Sahrens void
20899653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
209fa9e4066Sahrens {
210fa9e4066Sahrens 	va_list ap;
211fa9e4066Sahrens 
212fa9e4066Sahrens 	va_start(ap, fmt);
213fa9e4066Sahrens 
21499653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
21599653d4eSeschrock 	    fmt, ap);
21699653d4eSeschrock 	hdl->libzfs_desc_active = 1;
21799653d4eSeschrock 
21899653d4eSeschrock 	va_end(ap);
21999653d4eSeschrock }
22099653d4eSeschrock 
22199653d4eSeschrock static void
22299653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
22399653d4eSeschrock {
22499653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
22599653d4eSeschrock 	    fmt, ap);
22699653d4eSeschrock 	hdl->libzfs_error = error;
22799653d4eSeschrock 
22899653d4eSeschrock 	if (hdl->libzfs_desc_active)
22999653d4eSeschrock 		hdl->libzfs_desc_active = 0;
23099653d4eSeschrock 	else
23199653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
23299653d4eSeschrock 
23399653d4eSeschrock 	if (hdl->libzfs_printerr) {
23499653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
23599653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
23699653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
23799653d4eSeschrock 			abort();
23899653d4eSeschrock 		}
23999653d4eSeschrock 
24099653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
241b1b8ab34Slling 		    libzfs_error_description(hdl));
24299653d4eSeschrock 		if (error == EZFS_NOMEM)
24399653d4eSeschrock 			exit(1);
244fa9e4066Sahrens 	}
24599653d4eSeschrock }
24699653d4eSeschrock 
247ece3d9b3Slling int
248ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
249ece3d9b3Slling {
250ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
251ece3d9b3Slling }
252ece3d9b3Slling 
25399653d4eSeschrock /*PRINTFLIKE3*/
25499653d4eSeschrock int
255ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
25699653d4eSeschrock {
25799653d4eSeschrock 	va_list ap;
25899653d4eSeschrock 
25999653d4eSeschrock 	va_start(ap, fmt);
26099653d4eSeschrock 
26199653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
262fa9e4066Sahrens 
263fa9e4066Sahrens 	va_end(ap);
26499653d4eSeschrock 
26599653d4eSeschrock 	return (-1);
266fa9e4066Sahrens }
267fa9e4066Sahrens 
26899653d4eSeschrock static int
26999653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
27099653d4eSeschrock     va_list ap)
27199653d4eSeschrock {
27299653d4eSeschrock 	switch (error) {
27399653d4eSeschrock 	case EPERM:
27499653d4eSeschrock 	case EACCES:
27599653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
27699653d4eSeschrock 		return (-1);
27799653d4eSeschrock 
278*ecd6cf80Smarks 	case ECANCELED:
279*ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
280*ecd6cf80Smarks 		return (-1);
281*ecd6cf80Smarks 
28299653d4eSeschrock 	case EIO:
28399653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
28499653d4eSeschrock 		return (-1);
28599653d4eSeschrock 
28699653d4eSeschrock 	case EINTR:
28799653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
28899653d4eSeschrock 		return (-1);
28999653d4eSeschrock 	}
29099653d4eSeschrock 
29199653d4eSeschrock 	return (0);
29299653d4eSeschrock }
29399653d4eSeschrock 
294ece3d9b3Slling int
295ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
296ece3d9b3Slling {
297ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
298ece3d9b3Slling }
299ece3d9b3Slling 
30099653d4eSeschrock /*PRINTFLIKE3*/
30199653d4eSeschrock int
302ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	va_list ap;
305fa9e4066Sahrens 
306fa9e4066Sahrens 	va_start(ap, fmt);
307fa9e4066Sahrens 
30899653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
30999653d4eSeschrock 		va_end(ap);
31099653d4eSeschrock 		return (-1);
311fa9e4066Sahrens 	}
312fa9e4066Sahrens 
313fa9e4066Sahrens 
31499653d4eSeschrock 	switch (error) {
31599653d4eSeschrock 	case ENXIO:
31699653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
31799653d4eSeschrock 		break;
31899653d4eSeschrock 
31999653d4eSeschrock 	case ENOENT:
32099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32199653d4eSeschrock 		    "dataset does not exist"));
32299653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
32399653d4eSeschrock 		break;
32499653d4eSeschrock 
32599653d4eSeschrock 	case ENOSPC:
32699653d4eSeschrock 	case EDQUOT:
32799653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
32899653d4eSeschrock 		return (-1);
32999653d4eSeschrock 
33099653d4eSeschrock 	case EEXIST:
33199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33299653d4eSeschrock 		    "dataset already exists"));
33399653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
33499653d4eSeschrock 		break;
33599653d4eSeschrock 
33699653d4eSeschrock 	case EBUSY:
33799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33899653d4eSeschrock 		    "dataset is busy"));
33999653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
34099653d4eSeschrock 		break;
341*ecd6cf80Smarks 	case EROFS:
342*ecd6cf80Smarks 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
343*ecd6cf80Smarks 		    "snapshot permissions cannot be modified"));
344*ecd6cf80Smarks 		zfs_verror(hdl, EZFS_PERMRDONLY, fmt, ap);
345*ecd6cf80Smarks 		break;
346b7661cccSmmusante 	case ENAMETOOLONG:
347b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
348b7661cccSmmusante 		break;
34999653d4eSeschrock 	default:
35099653d4eSeschrock 		zfs_error_aux(hdl, strerror(errno));
35199653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
35299653d4eSeschrock 		break;
35399653d4eSeschrock 	}
35499653d4eSeschrock 
35599653d4eSeschrock 	va_end(ap);
35699653d4eSeschrock 	return (-1);
357fa9e4066Sahrens }
358fa9e4066Sahrens 
359ece3d9b3Slling int
360ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
361ece3d9b3Slling {
362ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
363ece3d9b3Slling }
364ece3d9b3Slling 
36599653d4eSeschrock /*PRINTFLIKE3*/
36699653d4eSeschrock int
367ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
368fa9e4066Sahrens {
36999653d4eSeschrock 	va_list ap;
37099653d4eSeschrock 
37199653d4eSeschrock 	va_start(ap, fmt);
37299653d4eSeschrock 
37399653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
37499653d4eSeschrock 		va_end(ap);
37599653d4eSeschrock 		return (-1);
37699653d4eSeschrock 	}
37799653d4eSeschrock 
37899653d4eSeschrock 	switch (error) {
37999653d4eSeschrock 	case ENODEV:
38099653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
38199653d4eSeschrock 		break;
38299653d4eSeschrock 
38399653d4eSeschrock 	case ENOENT:
384b1b8ab34Slling 		zfs_error_aux(hdl,
385b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
38699653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
38799653d4eSeschrock 		break;
38899653d4eSeschrock 
38999653d4eSeschrock 	case EEXIST:
39099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39199653d4eSeschrock 		    "pool already exists"));
39299653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
39399653d4eSeschrock 		break;
39499653d4eSeschrock 
39599653d4eSeschrock 	case EBUSY:
39699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
39799653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
39899653d4eSeschrock 		break;
39999653d4eSeschrock 
40099653d4eSeschrock 	case ENXIO:
40199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40299653d4eSeschrock 		    "one or more devices is currently unavailable"));
40399653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
40499653d4eSeschrock 		break;
40599653d4eSeschrock 
40699653d4eSeschrock 	case ENAMETOOLONG:
40799653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
40899653d4eSeschrock 		break;
40999653d4eSeschrock 
410b1b8ab34Slling 	case ENOTSUP:
411b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
412b1b8ab34Slling 		break;
413b1b8ab34Slling 
414b1b8ab34Slling 	case EINVAL:
415b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
416b1b8ab34Slling 		break;
417b1b8ab34Slling 
418*ecd6cf80Smarks 	case ENOSPC:
419*ecd6cf80Smarks 	case EDQUOT:
420*ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
421*ecd6cf80Smarks 		return (-1);
422*ecd6cf80Smarks 
42399653d4eSeschrock 	default:
42499653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
42599653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
42699653d4eSeschrock 	}
42799653d4eSeschrock 
42899653d4eSeschrock 	va_end(ap);
42999653d4eSeschrock 	return (-1);
430fa9e4066Sahrens }
431fa9e4066Sahrens 
432fa9e4066Sahrens /*
433fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
434fa9e4066Sahrens  */
43599653d4eSeschrock int
43699653d4eSeschrock no_memory(libzfs_handle_t *hdl)
437fa9e4066Sahrens {
43899653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
439fa9e4066Sahrens }
440fa9e4066Sahrens 
441fa9e4066Sahrens /*
442fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
443fa9e4066Sahrens  */
444fa9e4066Sahrens void *
44599653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
446fa9e4066Sahrens {
447fa9e4066Sahrens 	void *data;
448fa9e4066Sahrens 
449fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
45099653d4eSeschrock 		(void) no_memory(hdl);
451fa9e4066Sahrens 
452fa9e4066Sahrens 	return (data);
453fa9e4066Sahrens }
454fa9e4066Sahrens 
455e9dbad6fSeschrock /*
456e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
457e9dbad6fSeschrock  */
458e9dbad6fSeschrock void *
459e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
460e9dbad6fSeschrock {
461e9dbad6fSeschrock 	void *ret;
462e9dbad6fSeschrock 
463e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
464e9dbad6fSeschrock 		(void) no_memory(hdl);
465e9dbad6fSeschrock 		free(ptr);
466e9dbad6fSeschrock 		return (NULL);
467e9dbad6fSeschrock 	}
468e9dbad6fSeschrock 
469e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
470e9dbad6fSeschrock 	return (ret);
471e9dbad6fSeschrock }
472e9dbad6fSeschrock 
473fa9e4066Sahrens /*
474fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
475fa9e4066Sahrens  */
476fa9e4066Sahrens char *
47799653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
478fa9e4066Sahrens {
479fa9e4066Sahrens 	char *ret;
480fa9e4066Sahrens 
481fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
48299653d4eSeschrock 		(void) no_memory(hdl);
483fa9e4066Sahrens 
484fa9e4066Sahrens 	return (ret);
485fa9e4066Sahrens }
486fa9e4066Sahrens 
487fa9e4066Sahrens /*
488fa9e4066Sahrens  * Convert a number to an appropriately human-readable output.
489fa9e4066Sahrens  */
490fa9e4066Sahrens void
491fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
492fa9e4066Sahrens {
493fa9e4066Sahrens 	uint64_t n = num;
494fa9e4066Sahrens 	int index = 0;
495fa9e4066Sahrens 	char u;
496fa9e4066Sahrens 
497fa9e4066Sahrens 	while (n >= 1024) {
4985c709891Seschrock 		n /= 1024;
499fa9e4066Sahrens 		index++;
500fa9e4066Sahrens 	}
501fa9e4066Sahrens 
502fa9e4066Sahrens 	u = " KMGTPE"[index];
503fa9e4066Sahrens 
5045c709891Seschrock 	if (index == 0) {
505fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5065c709891Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5075c709891Seschrock 		/*
5085c709891Seschrock 		 * If this is an even multiple of the base, always display
5095c709891Seschrock 		 * without any decimal precision.
5105c709891Seschrock 		 */
511fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5125c709891Seschrock 	} else {
5135c709891Seschrock 		/*
5145c709891Seschrock 		 * We want to choose a precision that reflects the best choice
5155c709891Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5165c709891Seschrock 		 * we have numbers that are very close to an order of magnitude.
5175c709891Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5185c709891Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5195c709891Seschrock 		 * develop some complex heuristics for this, but it's much
5205c709891Seschrock 		 * easier just to try each combination in turn.
5215c709891Seschrock 		 */
5225c709891Seschrock 		int i;
5235c709891Seschrock 		for (i = 2; i >= 0; i--) {
5243d7072f8Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
5253d7072f8Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
5265c709891Seschrock 				break;
5275c709891Seschrock 		}
5285c709891Seschrock 	}
529fa9e4066Sahrens }
53099653d4eSeschrock 
53199653d4eSeschrock void
53299653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
53399653d4eSeschrock {
53499653d4eSeschrock 	hdl->libzfs_printerr = printerr;
53599653d4eSeschrock }
53699653d4eSeschrock 
53799653d4eSeschrock libzfs_handle_t *
53899653d4eSeschrock libzfs_init(void)
53999653d4eSeschrock {
54099653d4eSeschrock 	libzfs_handle_t *hdl;
54199653d4eSeschrock 
54299653d4eSeschrock 	if ((hdl = calloc(sizeof (libzfs_handle_t), 1)) == NULL) {
54399653d4eSeschrock 		return (NULL);
54499653d4eSeschrock 	}
54599653d4eSeschrock 
546c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
54799653d4eSeschrock 		free(hdl);
54899653d4eSeschrock 		return (NULL);
54999653d4eSeschrock 	}
55099653d4eSeschrock 
55199653d4eSeschrock 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
55299653d4eSeschrock 		(void) close(hdl->libzfs_fd);
55399653d4eSeschrock 		free(hdl);
55499653d4eSeschrock 		return (NULL);
55599653d4eSeschrock 	}
55699653d4eSeschrock 
55799653d4eSeschrock 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "r");
55899653d4eSeschrock 
55999653d4eSeschrock 	return (hdl);
56099653d4eSeschrock }
56199653d4eSeschrock 
56299653d4eSeschrock void
56399653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
56499653d4eSeschrock {
56599653d4eSeschrock 	(void) close(hdl->libzfs_fd);
56699653d4eSeschrock 	if (hdl->libzfs_mnttab)
56799653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
56899653d4eSeschrock 	if (hdl->libzfs_sharetab)
56999653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
57067331909Sdougm 	zfs_uninit_libshare(hdl);
571*ecd6cf80Smarks 	if (hdl->libzfs_log_str)
572*ecd6cf80Smarks 		(void) free(hdl->libzfs_log_str);
57399653d4eSeschrock 	namespace_clear(hdl);
57499653d4eSeschrock 	free(hdl);
57599653d4eSeschrock }
57699653d4eSeschrock 
57799653d4eSeschrock libzfs_handle_t *
57899653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
57999653d4eSeschrock {
58099653d4eSeschrock 	return (zhp->zpool_hdl);
58199653d4eSeschrock }
58299653d4eSeschrock 
58399653d4eSeschrock libzfs_handle_t *
58499653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
58599653d4eSeschrock {
58699653d4eSeschrock 	return (zhp->zfs_hdl);
58799653d4eSeschrock }
588e9dbad6fSeschrock 
5895aba80dbSck /*
5905aba80dbSck  * Given a name, determine whether or not it's a valid path
5915aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
5925aba80dbSck  * to match the device number.  If not, treat the path as an
5935aba80dbSck  * fs/vol/snap name.
5945aba80dbSck  */
5955aba80dbSck zfs_handle_t *
5965aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
5975aba80dbSck {
5985aba80dbSck 	struct stat64 statbuf;
5995aba80dbSck 	struct extmnttab entry;
6005aba80dbSck 	int ret;
6015aba80dbSck 
6025aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
6035aba80dbSck 		/*
6045aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
6055aba80dbSck 		 */
6065aba80dbSck 		return (zfs_open(hdl, path, argtype));
6075aba80dbSck 	}
6085aba80dbSck 
6095aba80dbSck 	if (stat64(path, &statbuf) != 0) {
6105aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
6115aba80dbSck 		return (NULL);
6125aba80dbSck 	}
6135aba80dbSck 
6145aba80dbSck 	rewind(hdl->libzfs_mnttab);
6155aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
6165aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
6175aba80dbSck 		    statbuf.st_dev) {
6185aba80dbSck 			break;
6195aba80dbSck 		}
6205aba80dbSck 	}
6215aba80dbSck 	if (ret != 0) {
6225aba80dbSck 		return (NULL);
6235aba80dbSck 	}
6245aba80dbSck 
6255aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6265aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
6275aba80dbSck 		    path);
6285aba80dbSck 		return (NULL);
6295aba80dbSck 	}
6305aba80dbSck 
6315aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
6325aba80dbSck }
6335aba80dbSck 
634e9dbad6fSeschrock /*
635e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
636e9dbad6fSeschrock  * an ioctl().
637e9dbad6fSeschrock  */
638e9dbad6fSeschrock int
639e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
640e9dbad6fSeschrock {
641e9dbad6fSeschrock 	if (len == 0)
642a2eea2e1Sahrens 		len = 2048;
643e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
644e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
645e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
646e9dbad6fSeschrock 		return (-1);
647e9dbad6fSeschrock 
648e9dbad6fSeschrock 	return (0);
649e9dbad6fSeschrock }
650e9dbad6fSeschrock 
651e9dbad6fSeschrock /*
652e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
653e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
654e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
655e9dbad6fSeschrock  */
656e9dbad6fSeschrock int
657e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
658e9dbad6fSeschrock {
659e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
660e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
661e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
662e9dbad6fSeschrock 	    == NULL)
663e9dbad6fSeschrock 		return (-1);
664e9dbad6fSeschrock 
665e9dbad6fSeschrock 	return (0);
666e9dbad6fSeschrock }
667e9dbad6fSeschrock 
668e9dbad6fSeschrock /*
669a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
670e9dbad6fSeschrock  */
671e9dbad6fSeschrock void
672e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
673e9dbad6fSeschrock {
674e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
675e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
676e9dbad6fSeschrock }
677e9dbad6fSeschrock 
678e9dbad6fSeschrock int
679e9dbad6fSeschrock zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl,
680e9dbad6fSeschrock     size_t *size)
681e9dbad6fSeschrock {
682e9dbad6fSeschrock 	char *packed;
683e9dbad6fSeschrock 	size_t len;
684e9dbad6fSeschrock 
685e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
686e9dbad6fSeschrock 
687e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
688e9dbad6fSeschrock 		return (-1);
689e9dbad6fSeschrock 
690e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
691e9dbad6fSeschrock 
692e9dbad6fSeschrock 	zc->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
693e9dbad6fSeschrock 	zc->zc_nvlist_src_size = len;
694e9dbad6fSeschrock 
695e9dbad6fSeschrock 	if (size)
696e9dbad6fSeschrock 		*size = len;
697e9dbad6fSeschrock 	return (0);
698e9dbad6fSeschrock }
699e9dbad6fSeschrock 
700e9dbad6fSeschrock /*
701e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
702e9dbad6fSeschrock  */
703e9dbad6fSeschrock int
704e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
705e9dbad6fSeschrock {
706e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
707e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
708e9dbad6fSeschrock 		return (no_memory(hdl));
709e9dbad6fSeschrock 
710e9dbad6fSeschrock 	return (0);
711e9dbad6fSeschrock }
712b1b8ab34Slling 
713b1b8ab34Slling static void
714b1b8ab34Slling zfs_print_prop_headers(libzfs_get_cbdata_t *cbp)
715b1b8ab34Slling {
716b1b8ab34Slling 	zfs_proplist_t *pl = cbp->cb_proplist;
717b1b8ab34Slling 	int i;
718b1b8ab34Slling 	char *title;
719b1b8ab34Slling 	size_t len;
720b1b8ab34Slling 
721b1b8ab34Slling 	cbp->cb_first = B_FALSE;
722b1b8ab34Slling 	if (cbp->cb_scripted)
723b1b8ab34Slling 		return;
724b1b8ab34Slling 
725b1b8ab34Slling 	/*
726b1b8ab34Slling 	 * Start with the length of the column headers.
727b1b8ab34Slling 	 */
728b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
729b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
730b1b8ab34Slling 	    "PROPERTY"));
731b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
732b1b8ab34Slling 	    "VALUE"));
733b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
734b1b8ab34Slling 	    "SOURCE"));
735b1b8ab34Slling 
736b1b8ab34Slling 	/*
737b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
738b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
739b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
740b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
741b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
742b1b8ab34Slling 	 * if the name of the property is much longer the any values we find.
743b1b8ab34Slling 	 */
744b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
745b1b8ab34Slling 		/*
746b1b8ab34Slling 		 * 'PROPERTY' column
747b1b8ab34Slling 		 */
748b1b8ab34Slling 		if (pl->pl_prop != ZFS_PROP_INVAL) {
749b1b8ab34Slling 			len = strlen(zfs_prop_to_name(pl->pl_prop));
750b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
751b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
752b1b8ab34Slling 		} else {
753b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
754b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
755b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
756b1b8ab34Slling 		}
757b1b8ab34Slling 
758b1b8ab34Slling 		/*
759b1b8ab34Slling 		 * 'VALUE' column
760b1b8ab34Slling 		 */
761b1b8ab34Slling 		if ((pl->pl_prop != ZFS_PROP_NAME || !pl->pl_all) &&
762b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
763b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
764b1b8ab34Slling 
765b1b8ab34Slling 		/*
766b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
767b1b8ab34Slling 		 */
768b1b8ab34Slling 		if (pl->pl_prop == ZFS_PROP_NAME &&
769b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
770b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
771b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
772b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
773b1b8ab34Slling 		}
774b1b8ab34Slling 	}
775b1b8ab34Slling 
776b1b8ab34Slling 	/*
777b1b8ab34Slling 	 * Now go through and print the headers.
778b1b8ab34Slling 	 */
779b1b8ab34Slling 	for (i = 0; i < 4; i++) {
780b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
781b1b8ab34Slling 		case GET_COL_NAME:
782b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
783b1b8ab34Slling 			break;
784b1b8ab34Slling 		case GET_COL_PROPERTY:
785b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
786b1b8ab34Slling 			break;
787b1b8ab34Slling 		case GET_COL_VALUE:
788b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
789b1b8ab34Slling 			break;
790b1b8ab34Slling 		case GET_COL_SOURCE:
791b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
792b1b8ab34Slling 			break;
793b1b8ab34Slling 		default:
794b1b8ab34Slling 			title = NULL;
795b1b8ab34Slling 		}
796b1b8ab34Slling 
797b1b8ab34Slling 		if (title != NULL) {
798b1b8ab34Slling 			if (i == 3 || cbp->cb_columns[i + 1] == 0)
799b1b8ab34Slling 				(void) printf("%s", title);
800b1b8ab34Slling 			else
801b1b8ab34Slling 				(void) printf("%-*s  ",
802b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
803b1b8ab34Slling 				    title);
804b1b8ab34Slling 		}
805b1b8ab34Slling 	}
806b1b8ab34Slling 	(void) printf("\n");
807b1b8ab34Slling }
808b1b8ab34Slling 
809b1b8ab34Slling /*
810b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
811b1b8ab34Slling  * structure.
812b1b8ab34Slling  */
813b1b8ab34Slling void
814b1b8ab34Slling libzfs_print_one_property(const char *name, libzfs_get_cbdata_t *cbp,
815b1b8ab34Slling     const char *propname, const char *value, zfs_source_t sourcetype,
816b1b8ab34Slling     const char *source)
817b1b8ab34Slling {
818b1b8ab34Slling 	int i;
819b1b8ab34Slling 	const char *str;
820b1b8ab34Slling 	char buf[128];
821b1b8ab34Slling 
822b1b8ab34Slling 	/*
823b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
824b1b8ab34Slling 	 */
825b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
826b1b8ab34Slling 		return;
827b1b8ab34Slling 
828b1b8ab34Slling 	if (cbp->cb_first)
829b1b8ab34Slling 		zfs_print_prop_headers(cbp);
830b1b8ab34Slling 
831b1b8ab34Slling 	for (i = 0; i < 4; i++) {
832b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
833b1b8ab34Slling 		case GET_COL_NAME:
834b1b8ab34Slling 			str = name;
835b1b8ab34Slling 			break;
836b1b8ab34Slling 
837b1b8ab34Slling 		case GET_COL_PROPERTY:
838b1b8ab34Slling 			str = propname;
839b1b8ab34Slling 			break;
840b1b8ab34Slling 
841b1b8ab34Slling 		case GET_COL_VALUE:
842b1b8ab34Slling 			str = value;
843b1b8ab34Slling 			break;
844b1b8ab34Slling 
845b1b8ab34Slling 		case GET_COL_SOURCE:
846b1b8ab34Slling 			switch (sourcetype) {
847b1b8ab34Slling 			case ZFS_SRC_NONE:
848b1b8ab34Slling 				str = "-";
849b1b8ab34Slling 				break;
850b1b8ab34Slling 
851b1b8ab34Slling 			case ZFS_SRC_DEFAULT:
852b1b8ab34Slling 				str = "default";
853b1b8ab34Slling 				break;
854b1b8ab34Slling 
855b1b8ab34Slling 			case ZFS_SRC_LOCAL:
856b1b8ab34Slling 				str = "local";
857b1b8ab34Slling 				break;
858b1b8ab34Slling 
859b1b8ab34Slling 			case ZFS_SRC_TEMPORARY:
860b1b8ab34Slling 				str = "temporary";
861b1b8ab34Slling 				break;
862b1b8ab34Slling 
863b1b8ab34Slling 			case ZFS_SRC_INHERITED:
864b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
865b1b8ab34Slling 				    "inherited from %s", source);
866b1b8ab34Slling 				str = buf;
867b1b8ab34Slling 				break;
868b1b8ab34Slling 			}
869b1b8ab34Slling 			break;
870b1b8ab34Slling 
871b1b8ab34Slling 		default:
872b1b8ab34Slling 			continue;
873b1b8ab34Slling 		}
874b1b8ab34Slling 
875b1b8ab34Slling 		if (cbp->cb_columns[i + 1] == 0)
876b1b8ab34Slling 			(void) printf("%s", str);
877b1b8ab34Slling 		else if (cbp->cb_scripted)
878b1b8ab34Slling 			(void) printf("%s\t", str);
879b1b8ab34Slling 		else
880b1b8ab34Slling 			(void) printf("%-*s  ",
881b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
882b1b8ab34Slling 			    str);
883b1b8ab34Slling 
884b1b8ab34Slling 	}
885b1b8ab34Slling 
886b1b8ab34Slling 	(void) printf("\n");
887b1b8ab34Slling }
888*ecd6cf80Smarks 
889*ecd6cf80Smarks int
890*ecd6cf80Smarks zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
891*ecd6cf80Smarks {
892*ecd6cf80Smarks 	int error;
893*ecd6cf80Smarks 
894*ecd6cf80Smarks 	zc->zc_history = (uint64_t)(uintptr_t)hdl->libzfs_log_str;
895*ecd6cf80Smarks 	zc->zc_history_offset = hdl->libzfs_log_type;
896*ecd6cf80Smarks 	error = ioctl(hdl->libzfs_fd, request, zc);
897*ecd6cf80Smarks 	if (hdl->libzfs_log_str) {
898*ecd6cf80Smarks 		free(hdl->libzfs_log_str);
899*ecd6cf80Smarks 		hdl->libzfs_log_str = NULL;
900*ecd6cf80Smarks 	}
901*ecd6cf80Smarks 	zc->zc_history = 0;
902*ecd6cf80Smarks 	zc->zc_history_offset = 0;
903*ecd6cf80Smarks 
904*ecd6cf80Smarks 	return (error);
905*ecd6cf80Smarks }
906