xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_util.c (revision 2bcf0248e992f292c7b814458bcdce2f004925d6)
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  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24a2afb611SJerry Jelinek  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25*2bcf0248SMax Grossman  * Copyright (c) 2014 by Delphix. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * Internal utility routines for the ZFS library.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens #include <errno.h>
33fa9e4066Sahrens #include <fcntl.h>
34fa9e4066Sahrens #include <libintl.h>
35fa9e4066Sahrens #include <stdarg.h>
36fa9e4066Sahrens #include <stdio.h>
37fa9e4066Sahrens #include <stdlib.h>
38fa9e4066Sahrens #include <strings.h>
39fa9e4066Sahrens #include <unistd.h>
40990b4856Slling #include <ctype.h>
41990b4856Slling #include <math.h>
42*2bcf0248SMax Grossman #include <sys/filio.h>
43fa9e4066Sahrens #include <sys/mnttab.h>
445aba80dbSck #include <sys/mntent.h>
455aba80dbSck #include <sys/types.h>
46fa9e4066Sahrens 
47fa9e4066Sahrens #include <libzfs.h>
484445fffbSMatthew Ahrens #include <libzfs_core.h>
49fa9e4066Sahrens 
50fa9e4066Sahrens #include "libzfs_impl.h"
5191ebeef5Sahrens #include "zfs_prop.h"
52ad135b5dSChristopher Siden #include "zfeature_common.h"
53fa9e4066Sahrens 
5499653d4eSeschrock int
5599653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
5699653d4eSeschrock {
5799653d4eSeschrock 	return (hdl->libzfs_error);
5899653d4eSeschrock }
59fa9e4066Sahrens 
6099653d4eSeschrock const char *
6199653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
6299653d4eSeschrock {
6399653d4eSeschrock 	return (hdl->libzfs_action);
6499653d4eSeschrock }
65fa9e4066Sahrens 
6699653d4eSeschrock const char *
6799653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl)
6899653d4eSeschrock {
6999653d4eSeschrock 	if (hdl->libzfs_desc[0] != '\0')
7099653d4eSeschrock 		return (hdl->libzfs_desc);
7199653d4eSeschrock 
7299653d4eSeschrock 	switch (hdl->libzfs_error) {
7399653d4eSeschrock 	case EZFS_NOMEM:
7499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
7599653d4eSeschrock 	case EZFS_BADPROP:
7699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
7799653d4eSeschrock 	case EZFS_PROPREADONLY:
78f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "read-only property"));
7999653d4eSeschrock 	case EZFS_PROPTYPE:
8099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
8199653d4eSeschrock 		    "datasets of this type"));
8299653d4eSeschrock 	case EZFS_PROPNONINHERIT:
8399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
8499653d4eSeschrock 	case EZFS_PROPSPACE:
8599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
8699653d4eSeschrock 	case EZFS_BADTYPE:
8799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
8899653d4eSeschrock 		    "datasets of this type"));
8999653d4eSeschrock 	case EZFS_BUSY:
9099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
9199653d4eSeschrock 	case EZFS_EXISTS:
9299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
9399653d4eSeschrock 	case EZFS_NOENT:
9499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
9599653d4eSeschrock 	case EZFS_BADSTREAM:
9699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
9799653d4eSeschrock 	case EZFS_DSREADONLY:
98f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
9999653d4eSeschrock 	case EZFS_VOLTOOBIG:
10099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
10199653d4eSeschrock 		    "this system"));
10299653d4eSeschrock 	case EZFS_INVALIDNAME:
10399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
10499653d4eSeschrock 	case EZFS_BADRESTORE:
10599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
10699653d4eSeschrock 		    "destination"));
10799653d4eSeschrock 	case EZFS_BADBACKUP:
10899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
10999653d4eSeschrock 	case EZFS_BADTARGET:
11099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
11199653d4eSeschrock 	case EZFS_NODEVICE:
11299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
11399653d4eSeschrock 	case EZFS_BADDEV:
11499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
11599653d4eSeschrock 	case EZFS_NOREPLICAS:
11699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
11799653d4eSeschrock 	case EZFS_RESILVERING:
11899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
11999653d4eSeschrock 	case EZFS_BADVERSION:
120ad135b5dSChristopher Siden 		return (dgettext(TEXT_DOMAIN, "unsupported version or "
121ad135b5dSChristopher Siden 		    "feature"));
12299653d4eSeschrock 	case EZFS_POOLUNAVAIL:
12399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
12499653d4eSeschrock 	case EZFS_DEVOVERFLOW:
12599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
12699653d4eSeschrock 	case EZFS_BADPATH:
12799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
12899653d4eSeschrock 	case EZFS_CROSSTARGET:
12999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
13099653d4eSeschrock 		    "pools"));
13199653d4eSeschrock 	case EZFS_ZONED:
13299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
13399653d4eSeschrock 	case EZFS_MOUNTFAILED:
13499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
13599653d4eSeschrock 	case EZFS_UMOUNTFAILED:
13699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
137f3861e1aSahl 	case EZFS_UNSHARENFSFAILED:
13899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
139f3861e1aSahl 	case EZFS_SHARENFSFAILED:
14099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
141da6c28aaSamw 	case EZFS_UNSHARESMBFAILED:
142da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
143da6c28aaSamw 	case EZFS_SHARESMBFAILED:
144da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
14599653d4eSeschrock 	case EZFS_PERM:
14699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
14799653d4eSeschrock 	case EZFS_NOSPC:
14899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
1496e27f868SSam Falkner 	case EZFS_FAULT:
1506e27f868SSam Falkner 		return (dgettext(TEXT_DOMAIN, "bad address"));
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"));
164b1b8ab34Slling 	case EZFS_POOLPROPS:
165b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
166b1b8ab34Slling 		    "pool properties"));
167b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
168b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
169b1b8ab34Slling 		    "on this type of pool"));
170b1b8ab34Slling 	case EZFS_POOL_INVALARG:
171b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
172b1b8ab34Slling 		    "this pool operation"));
173b7661cccSmmusante 	case EZFS_NAMETOOLONG:
174b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1758488aeb5Staylor 	case EZFS_OPENFAILED:
1768488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1778488aeb5Staylor 	case EZFS_NOCAP:
1788488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1798488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1808488aeb5Staylor 	case EZFS_LABELFAILED:
1818488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
182ecd6cf80Smarks 	case EZFS_BADWHO:
183ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
184ecd6cf80Smarks 	case EZFS_BADPERM:
185ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
186ecd6cf80Smarks 	case EZFS_BADPERMSET:
187ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
188ecd6cf80Smarks 	case EZFS_NODELEGATION:
189ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
190ecd6cf80Smarks 		    "disabled on pool"));
1912f8aaab3Seschrock 	case EZFS_BADCACHE:
1922f8aaab3Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
193fa94a07fSbrendan 	case EZFS_ISL2CACHE:
194fa94a07fSbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
195e7cbe64fSgw 	case EZFS_VDEVNOTSUP:
196e7cbe64fSgw 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
197e7cbe64fSgw 		    "supported"));
19815e6edf1Sgw 	case EZFS_NOTSUP:
19915e6edf1Sgw 		return (dgettext(TEXT_DOMAIN, "operation not supported "
20015e6edf1Sgw 		    "on this dataset"));
20189a89ebfSlling 	case EZFS_ACTIVE_SPARE:
20289a89ebfSlling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
20389a89ebfSlling 		    "device"));
204e6ca193dSGeorge Wilson 	case EZFS_UNPLAYED_LOGS:
205e6ca193dSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
206e6ca193dSGeorge Wilson 		    "logs"));
207842727c2SChris Kirby 	case EZFS_REFTAG_RELE:
208842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
209842727c2SChris Kirby 	case EZFS_REFTAG_HOLD:
210842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
211842727c2SChris Kirby 		    "dataset"));
212ca45db41SChris Kirby 	case EZFS_TAGTOOLONG:
213ca45db41SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag too long"));
2149e69d7d0SLori Alt 	case EZFS_PIPEFAILED:
2159e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
2169e69d7d0SLori Alt 	case EZFS_THREADCREATEFAILED:
2179e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "thread create failed"));
2181195e687SMark J Musante 	case EZFS_POSTSPLIT_ONLINE:
2191195e687SMark J Musante 		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
2201195e687SMark J Musante 		    "into a new one"));
2213f9d6ad7SLin Ling 	case EZFS_SCRUBBING:
2223f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
2233f9d6ad7SLin Ling 		    "use 'zpool scrub -s' to cancel current scrub"));
2243f9d6ad7SLin Ling 	case EZFS_NO_SCRUB:
2253f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
22699d5e173STim Haley 	case EZFS_DIFF:
22799d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
22899d5e173STim Haley 	case EZFS_DIFFDATA:
22999d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
230f9af39baSGeorge Wilson 	case EZFS_POOLREADONLY:
231f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
23299653d4eSeschrock 	case EZFS_UNKNOWN:
23399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
23499653d4eSeschrock 	default:
235c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
236c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
23799653d4eSeschrock 	}
23899653d4eSeschrock }
23999653d4eSeschrock 
24099653d4eSeschrock /*PRINTFLIKE2*/
241fa9e4066Sahrens void
24299653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
243fa9e4066Sahrens {
244fa9e4066Sahrens 	va_list ap;
245fa9e4066Sahrens 
246fa9e4066Sahrens 	va_start(ap, fmt);
247fa9e4066Sahrens 
24899653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
24999653d4eSeschrock 	    fmt, ap);
25099653d4eSeschrock 	hdl->libzfs_desc_active = 1;
25199653d4eSeschrock 
25299653d4eSeschrock 	va_end(ap);
25399653d4eSeschrock }
25499653d4eSeschrock 
25599653d4eSeschrock static void
25699653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
25799653d4eSeschrock {
25899653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
25999653d4eSeschrock 	    fmt, ap);
26099653d4eSeschrock 	hdl->libzfs_error = error;
26199653d4eSeschrock 
26299653d4eSeschrock 	if (hdl->libzfs_desc_active)
26399653d4eSeschrock 		hdl->libzfs_desc_active = 0;
26499653d4eSeschrock 	else
26599653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
26699653d4eSeschrock 
26799653d4eSeschrock 	if (hdl->libzfs_printerr) {
26899653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
26999653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
27099653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
27199653d4eSeschrock 			abort();
27299653d4eSeschrock 		}
27399653d4eSeschrock 
27499653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
275b1b8ab34Slling 		    libzfs_error_description(hdl));
27699653d4eSeschrock 		if (error == EZFS_NOMEM)
27799653d4eSeschrock 			exit(1);
278fa9e4066Sahrens 	}
27999653d4eSeschrock }
28099653d4eSeschrock 
281ece3d9b3Slling int
282ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
283ece3d9b3Slling {
284ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
285ece3d9b3Slling }
286ece3d9b3Slling 
28799653d4eSeschrock /*PRINTFLIKE3*/
28899653d4eSeschrock int
289ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
29099653d4eSeschrock {
29199653d4eSeschrock 	va_list ap;
29299653d4eSeschrock 
29399653d4eSeschrock 	va_start(ap, fmt);
29499653d4eSeschrock 
29599653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
296fa9e4066Sahrens 
297fa9e4066Sahrens 	va_end(ap);
29899653d4eSeschrock 
29999653d4eSeschrock 	return (-1);
300fa9e4066Sahrens }
301fa9e4066Sahrens 
30299653d4eSeschrock static int
30399653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
30499653d4eSeschrock     va_list ap)
30599653d4eSeschrock {
30699653d4eSeschrock 	switch (error) {
30799653d4eSeschrock 	case EPERM:
30899653d4eSeschrock 	case EACCES:
30999653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
31099653d4eSeschrock 		return (-1);
31199653d4eSeschrock 
312ecd6cf80Smarks 	case ECANCELED:
313ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
314ecd6cf80Smarks 		return (-1);
315ecd6cf80Smarks 
31699653d4eSeschrock 	case EIO:
31799653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
31899653d4eSeschrock 		return (-1);
31999653d4eSeschrock 
3206e27f868SSam Falkner 	case EFAULT:
3216e27f868SSam Falkner 		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
3226e27f868SSam Falkner 		return (-1);
3236e27f868SSam Falkner 
32499653d4eSeschrock 	case EINTR:
32599653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
32699653d4eSeschrock 		return (-1);
32799653d4eSeschrock 	}
32899653d4eSeschrock 
32999653d4eSeschrock 	return (0);
33099653d4eSeschrock }
33199653d4eSeschrock 
332ece3d9b3Slling int
333ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
334ece3d9b3Slling {
335ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
336ece3d9b3Slling }
337ece3d9b3Slling 
33899653d4eSeschrock /*PRINTFLIKE3*/
33999653d4eSeschrock int
340ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
341fa9e4066Sahrens {
342fa9e4066Sahrens 	va_list ap;
343fa9e4066Sahrens 
344fa9e4066Sahrens 	va_start(ap, fmt);
345fa9e4066Sahrens 
34699653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
34799653d4eSeschrock 		va_end(ap);
34899653d4eSeschrock 		return (-1);
349fa9e4066Sahrens 	}
350fa9e4066Sahrens 
35199653d4eSeschrock 	switch (error) {
35299653d4eSeschrock 	case ENXIO:
35397d9e3a6Sck 	case ENODEV:
35419b94df9SMatthew Ahrens 	case EPIPE:
35599653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
35699653d4eSeschrock 		break;
35799653d4eSeschrock 
35899653d4eSeschrock 	case ENOENT:
35999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36099653d4eSeschrock 		    "dataset does not exist"));
36199653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
36299653d4eSeschrock 		break;
36399653d4eSeschrock 
36499653d4eSeschrock 	case ENOSPC:
36599653d4eSeschrock 	case EDQUOT:
36699653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
36799653d4eSeschrock 		return (-1);
36899653d4eSeschrock 
36999653d4eSeschrock 	case EEXIST:
37099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37199653d4eSeschrock 		    "dataset already exists"));
37299653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
37399653d4eSeschrock 		break;
37499653d4eSeschrock 
37599653d4eSeschrock 	case EBUSY:
37699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37799653d4eSeschrock 		    "dataset is busy"));
37899653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
37999653d4eSeschrock 		break;
380ecd6cf80Smarks 	case EROFS:
381f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
382ecd6cf80Smarks 		break;
383b7661cccSmmusante 	case ENAMETOOLONG:
384b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
385b7661cccSmmusante 		break;
38640ff3960Sck 	case ENOTSUP:
38740ff3960Sck 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
38840ff3960Sck 		break;
38954d692b7SGeorge Wilson 	case EAGAIN:
39054d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39154d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
39254d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
39354d692b7SGeorge Wilson 		break;
39499653d4eSeschrock 	default:
39592241e0bSTom Erickson 		zfs_error_aux(hdl, strerror(error));
39699653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
39799653d4eSeschrock 		break;
39899653d4eSeschrock 	}
39999653d4eSeschrock 
40099653d4eSeschrock 	va_end(ap);
40199653d4eSeschrock 	return (-1);
402fa9e4066Sahrens }
403fa9e4066Sahrens 
404ece3d9b3Slling int
405ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
406ece3d9b3Slling {
407ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
408ece3d9b3Slling }
409ece3d9b3Slling 
41099653d4eSeschrock /*PRINTFLIKE3*/
41199653d4eSeschrock int
412ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
413fa9e4066Sahrens {
41499653d4eSeschrock 	va_list ap;
41599653d4eSeschrock 
41699653d4eSeschrock 	va_start(ap, fmt);
41799653d4eSeschrock 
41899653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
41999653d4eSeschrock 		va_end(ap);
42099653d4eSeschrock 		return (-1);
42199653d4eSeschrock 	}
42299653d4eSeschrock 
42399653d4eSeschrock 	switch (error) {
42499653d4eSeschrock 	case ENODEV:
42599653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
42699653d4eSeschrock 		break;
42799653d4eSeschrock 
42899653d4eSeschrock 	case ENOENT:
429b1b8ab34Slling 		zfs_error_aux(hdl,
430b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
43199653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
43299653d4eSeschrock 		break;
43399653d4eSeschrock 
43499653d4eSeschrock 	case EEXIST:
43599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43699653d4eSeschrock 		    "pool already exists"));
43799653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
43899653d4eSeschrock 		break;
43999653d4eSeschrock 
44099653d4eSeschrock 	case EBUSY:
44199653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
44243afaaa8SEric Schrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
44399653d4eSeschrock 		break;
44499653d4eSeschrock 
44599653d4eSeschrock 	case ENXIO:
44699653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44799653d4eSeschrock 		    "one or more devices is currently unavailable"));
44899653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
44999653d4eSeschrock 		break;
45099653d4eSeschrock 
45199653d4eSeschrock 	case ENAMETOOLONG:
45299653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
45399653d4eSeschrock 		break;
45499653d4eSeschrock 
455b1b8ab34Slling 	case ENOTSUP:
456b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
457b1b8ab34Slling 		break;
458b1b8ab34Slling 
459b1b8ab34Slling 	case EINVAL:
460b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
461b1b8ab34Slling 		break;
462b1b8ab34Slling 
463ecd6cf80Smarks 	case ENOSPC:
464ecd6cf80Smarks 	case EDQUOT:
465ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
466ecd6cf80Smarks 		return (-1);
467f9af39baSGeorge Wilson 
46854d692b7SGeorge Wilson 	case EAGAIN:
46954d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
47054d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
47154d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
47254d692b7SGeorge Wilson 		break;
473ecd6cf80Smarks 
474f9af39baSGeorge Wilson 	case EROFS:
475f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
476f9af39baSGeorge Wilson 		break;
477f9af39baSGeorge Wilson 
47899653d4eSeschrock 	default:
47999653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
48099653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
48199653d4eSeschrock 	}
48299653d4eSeschrock 
48399653d4eSeschrock 	va_end(ap);
48499653d4eSeschrock 	return (-1);
485fa9e4066Sahrens }
486fa9e4066Sahrens 
487fa9e4066Sahrens /*
488fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
489fa9e4066Sahrens  */
49099653d4eSeschrock int
49199653d4eSeschrock no_memory(libzfs_handle_t *hdl)
492fa9e4066Sahrens {
49399653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
494fa9e4066Sahrens }
495fa9e4066Sahrens 
496fa9e4066Sahrens /*
497fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
498fa9e4066Sahrens  */
499fa9e4066Sahrens void *
50099653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
501fa9e4066Sahrens {
502fa9e4066Sahrens 	void *data;
503fa9e4066Sahrens 
504fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
50599653d4eSeschrock 		(void) no_memory(hdl);
506fa9e4066Sahrens 
507fa9e4066Sahrens 	return (data);
508fa9e4066Sahrens }
509fa9e4066Sahrens 
51099d5e173STim Haley /*
51199d5e173STim Haley  * A safe form of asprintf() which will die if the allocation fails.
51299d5e173STim Haley  */
51399d5e173STim Haley /*PRINTFLIKE2*/
51499d5e173STim Haley char *
51599d5e173STim Haley zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
51699d5e173STim Haley {
51799d5e173STim Haley 	va_list ap;
51899d5e173STim Haley 	char *ret;
51999d5e173STim Haley 	int err;
52099d5e173STim Haley 
52199d5e173STim Haley 	va_start(ap, fmt);
52299d5e173STim Haley 
52399d5e173STim Haley 	err = vasprintf(&ret, fmt, ap);
52499d5e173STim Haley 
52599d5e173STim Haley 	va_end(ap);
52699d5e173STim Haley 
52799d5e173STim Haley 	if (err < 0)
52899d5e173STim Haley 		(void) no_memory(hdl);
52999d5e173STim Haley 
53099d5e173STim Haley 	return (ret);
53199d5e173STim Haley }
53299d5e173STim Haley 
533e9dbad6fSeschrock /*
534e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
535e9dbad6fSeschrock  */
536e9dbad6fSeschrock void *
537e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
538e9dbad6fSeschrock {
539e9dbad6fSeschrock 	void *ret;
540e9dbad6fSeschrock 
541e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
542e9dbad6fSeschrock 		(void) no_memory(hdl);
543e9dbad6fSeschrock 		return (NULL);
544e9dbad6fSeschrock 	}
545e9dbad6fSeschrock 
546e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
547e9dbad6fSeschrock 	return (ret);
548e9dbad6fSeschrock }
549e9dbad6fSeschrock 
550fa9e4066Sahrens /*
551fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
552fa9e4066Sahrens  */
553fa9e4066Sahrens char *
55499653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
555fa9e4066Sahrens {
556fa9e4066Sahrens 	char *ret;
557fa9e4066Sahrens 
558fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
55999653d4eSeschrock 		(void) no_memory(hdl);
560fa9e4066Sahrens 
561fa9e4066Sahrens 	return (ret);
562fa9e4066Sahrens }
563fa9e4066Sahrens 
564fa9e4066Sahrens /*
565fa9e4066Sahrens  * Convert a number to an appropriately human-readable output.
566fa9e4066Sahrens  */
567fa9e4066Sahrens void
568fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
569fa9e4066Sahrens {
570fa9e4066Sahrens 	uint64_t n = num;
571fa9e4066Sahrens 	int index = 0;
572fa9e4066Sahrens 	char u;
573fa9e4066Sahrens 
574fa9e4066Sahrens 	while (n >= 1024) {
5755c709891Seschrock 		n /= 1024;
576fa9e4066Sahrens 		index++;
577fa9e4066Sahrens 	}
578fa9e4066Sahrens 
579fa9e4066Sahrens 	u = " KMGTPE"[index];
580fa9e4066Sahrens 
5815c709891Seschrock 	if (index == 0) {
582fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu", n);
5835c709891Seschrock 	} else if ((num & ((1ULL << 10 * index) - 1)) == 0) {
5845c709891Seschrock 		/*
5855c709891Seschrock 		 * If this is an even multiple of the base, always display
5865c709891Seschrock 		 * without any decimal precision.
5875c709891Seschrock 		 */
588fa9e4066Sahrens 		(void) snprintf(buf, buflen, "%llu%c", n, u);
5895c709891Seschrock 	} else {
5905c709891Seschrock 		/*
5915c709891Seschrock 		 * We want to choose a precision that reflects the best choice
5925c709891Seschrock 		 * for fitting in 5 characters.  This can get rather tricky when
5935c709891Seschrock 		 * we have numbers that are very close to an order of magnitude.
5945c709891Seschrock 		 * For example, when displaying 10239 (which is really 9.999K),
5955c709891Seschrock 		 * we want only a single place of precision for 10.0K.  We could
5965c709891Seschrock 		 * develop some complex heuristics for this, but it's much
5975c709891Seschrock 		 * easier just to try each combination in turn.
5985c709891Seschrock 		 */
5995c709891Seschrock 		int i;
6005c709891Seschrock 		for (i = 2; i >= 0; i--) {
6013d7072f8Seschrock 			if (snprintf(buf, buflen, "%.*f%c", i,
6023d7072f8Seschrock 			    (double)num / (1ULL << 10 * index), u) <= 5)
6035c709891Seschrock 				break;
6045c709891Seschrock 		}
6055c709891Seschrock 	}
606fa9e4066Sahrens }
60799653d4eSeschrock 
60899653d4eSeschrock void
60999653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
61099653d4eSeschrock {
61199653d4eSeschrock 	hdl->libzfs_printerr = printerr;
61299653d4eSeschrock }
61399653d4eSeschrock 
61499653d4eSeschrock libzfs_handle_t *
61599653d4eSeschrock libzfs_init(void)
61699653d4eSeschrock {
61799653d4eSeschrock 	libzfs_handle_t *hdl;
61899653d4eSeschrock 
61999d5e173STim Haley 	if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
62099653d4eSeschrock 		return (NULL);
62199653d4eSeschrock 	}
62299653d4eSeschrock 
623c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
62499653d4eSeschrock 		free(hdl);
62599653d4eSeschrock 		return (NULL);
62699653d4eSeschrock 	}
62799653d4eSeschrock 
628bde3d612SSimon Klinkert 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "rF")) == NULL) {
62999653d4eSeschrock 		(void) close(hdl->libzfs_fd);
63099653d4eSeschrock 		free(hdl);
63199653d4eSeschrock 		return (NULL);
63299653d4eSeschrock 	}
63399653d4eSeschrock 
634bde3d612SSimon Klinkert 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "rF");
63599653d4eSeschrock 
6364445fffbSMatthew Ahrens 	if (libzfs_core_init() != 0) {
6374445fffbSMatthew Ahrens 		(void) close(hdl->libzfs_fd);
6384445fffbSMatthew Ahrens 		(void) fclose(hdl->libzfs_mnttab);
6394445fffbSMatthew Ahrens 		(void) fclose(hdl->libzfs_sharetab);
6404445fffbSMatthew Ahrens 		free(hdl);
6414445fffbSMatthew Ahrens 		return (NULL);
6424445fffbSMatthew Ahrens 	}
6434445fffbSMatthew Ahrens 
64491ebeef5Sahrens 	zfs_prop_init();
645990b4856Slling 	zpool_prop_init();
646ad135b5dSChristopher Siden 	zpool_feature_init();
647b2634b9cSEric Taylor 	libzfs_mnttab_init(hdl);
64891ebeef5Sahrens 
64999653d4eSeschrock 	return (hdl);
65099653d4eSeschrock }
65199653d4eSeschrock 
65299653d4eSeschrock void
65399653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
65499653d4eSeschrock {
65599653d4eSeschrock 	(void) close(hdl->libzfs_fd);
65699653d4eSeschrock 	if (hdl->libzfs_mnttab)
65799653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
65899653d4eSeschrock 	if (hdl->libzfs_sharetab)
65999653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
66067331909Sdougm 	zfs_uninit_libshare(hdl);
66129ab75c9Srm 	zpool_free_handles(hdl);
662069f55e2SEric Schrock 	libzfs_fru_clear(hdl, B_TRUE);
66399653d4eSeschrock 	namespace_clear(hdl);
664b2634b9cSEric Taylor 	libzfs_mnttab_fini(hdl);
6654445fffbSMatthew Ahrens 	libzfs_core_fini();
66699653d4eSeschrock 	free(hdl);
66799653d4eSeschrock }
66899653d4eSeschrock 
66999653d4eSeschrock libzfs_handle_t *
67099653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
67199653d4eSeschrock {
67299653d4eSeschrock 	return (zhp->zpool_hdl);
67399653d4eSeschrock }
67499653d4eSeschrock 
67599653d4eSeschrock libzfs_handle_t *
67699653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
67799653d4eSeschrock {
67899653d4eSeschrock 	return (zhp->zfs_hdl);
67999653d4eSeschrock }
680e9dbad6fSeschrock 
681d5b5bb25SRich Morris zpool_handle_t *
682d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp)
683d5b5bb25SRich Morris {
684d5b5bb25SRich Morris 	return (zhp->zpool_hdl);
685d5b5bb25SRich Morris }
686d5b5bb25SRich Morris 
6875aba80dbSck /*
6885aba80dbSck  * Given a name, determine whether or not it's a valid path
6895aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
6905aba80dbSck  * to match the device number.  If not, treat the path as an
6915aba80dbSck  * fs/vol/snap name.
6925aba80dbSck  */
6935aba80dbSck zfs_handle_t *
6945aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
6955aba80dbSck {
6965aba80dbSck 	struct stat64 statbuf;
6975aba80dbSck 	struct extmnttab entry;
6985aba80dbSck 	int ret;
6995aba80dbSck 
7005aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
7015aba80dbSck 		/*
7025aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
7035aba80dbSck 		 */
7045aba80dbSck 		return (zfs_open(hdl, path, argtype));
7055aba80dbSck 	}
7065aba80dbSck 
7075aba80dbSck 	if (stat64(path, &statbuf) != 0) {
7085aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
7095aba80dbSck 		return (NULL);
7105aba80dbSck 	}
7115aba80dbSck 
7125aba80dbSck 	rewind(hdl->libzfs_mnttab);
7135aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
7145aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
7155aba80dbSck 		    statbuf.st_dev) {
7165aba80dbSck 			break;
7175aba80dbSck 		}
7185aba80dbSck 	}
7195aba80dbSck 	if (ret != 0) {
7205aba80dbSck 		return (NULL);
7215aba80dbSck 	}
7225aba80dbSck 
7235aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7245aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
7255aba80dbSck 		    path);
7265aba80dbSck 		return (NULL);
7275aba80dbSck 	}
7285aba80dbSck 
7295aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
7305aba80dbSck }
7315aba80dbSck 
732e9dbad6fSeschrock /*
733e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
734e9dbad6fSeschrock  * an ioctl().
735e9dbad6fSeschrock  */
736e9dbad6fSeschrock int
737e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
738e9dbad6fSeschrock {
739e9dbad6fSeschrock 	if (len == 0)
7404b964adaSGeorge Wilson 		len = 16 * 1024;
741e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
742e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
743e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size)) == NULL)
744e9dbad6fSeschrock 		return (-1);
745e9dbad6fSeschrock 
746e9dbad6fSeschrock 	return (0);
747e9dbad6fSeschrock }
748e9dbad6fSeschrock 
749e9dbad6fSeschrock /*
750e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
751e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
752e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
753e9dbad6fSeschrock  */
754e9dbad6fSeschrock int
755e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
756e9dbad6fSeschrock {
757e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
758e9dbad6fSeschrock 	if ((zc->zc_nvlist_dst = (uint64_t)(uintptr_t)
759e9dbad6fSeschrock 	    zfs_alloc(hdl, zc->zc_nvlist_dst_size))
760e9dbad6fSeschrock 	    == NULL)
761e9dbad6fSeschrock 		return (-1);
762e9dbad6fSeschrock 
763e9dbad6fSeschrock 	return (0);
764e9dbad6fSeschrock }
765e9dbad6fSeschrock 
766e9dbad6fSeschrock /*
767a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
768e9dbad6fSeschrock  */
769e9dbad6fSeschrock void
770e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
771e9dbad6fSeschrock {
772990b4856Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
773e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
774e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
775e9dbad6fSeschrock }
776e9dbad6fSeschrock 
777990b4856Slling static int
778990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
779990b4856Slling     nvlist_t *nvl)
780e9dbad6fSeschrock {
781e9dbad6fSeschrock 	char *packed;
782e9dbad6fSeschrock 	size_t len;
783e9dbad6fSeschrock 
784e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
785e9dbad6fSeschrock 
786e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
787e9dbad6fSeschrock 		return (-1);
788e9dbad6fSeschrock 
789e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
790e9dbad6fSeschrock 
791990b4856Slling 	*outnv = (uint64_t)(uintptr_t)packed;
792990b4856Slling 	*outlen = len;
793e9dbad6fSeschrock 
794e9dbad6fSeschrock 	return (0);
795e9dbad6fSeschrock }
796e9dbad6fSeschrock 
797990b4856Slling int
798990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
799990b4856Slling {
800990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
801990b4856Slling 	    &zc->zc_nvlist_conf_size, nvl));
802990b4856Slling }
803990b4856Slling 
804990b4856Slling int
805990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
806990b4856Slling {
807990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
808990b4856Slling 	    &zc->zc_nvlist_src_size, nvl));
809990b4856Slling }
810990b4856Slling 
811e9dbad6fSeschrock /*
812e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
813e9dbad6fSeschrock  */
814e9dbad6fSeschrock int
815e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
816e9dbad6fSeschrock {
817e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
818e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
819e9dbad6fSeschrock 		return (no_memory(hdl));
820e9dbad6fSeschrock 
821e9dbad6fSeschrock 	return (0);
822e9dbad6fSeschrock }
823b1b8ab34Slling 
824990b4856Slling int
825990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
826990b4856Slling {
8274445fffbSMatthew Ahrens 	return (ioctl(hdl->libzfs_fd, request, zc));
828990b4856Slling }
829990b4856Slling 
830990b4856Slling /*
831990b4856Slling  * ================================================================
832990b4856Slling  * API shared by zfs and zpool property management
833990b4856Slling  * ================================================================
834990b4856Slling  */
835990b4856Slling 
836b1b8ab34Slling static void
837990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
838b1b8ab34Slling {
839990b4856Slling 	zprop_list_t *pl = cbp->cb_proplist;
840b1b8ab34Slling 	int i;
841b1b8ab34Slling 	char *title;
842b1b8ab34Slling 	size_t len;
843b1b8ab34Slling 
844b1b8ab34Slling 	cbp->cb_first = B_FALSE;
845b1b8ab34Slling 	if (cbp->cb_scripted)
846b1b8ab34Slling 		return;
847b1b8ab34Slling 
848b1b8ab34Slling 	/*
849b1b8ab34Slling 	 * Start with the length of the column headers.
850b1b8ab34Slling 	 */
851b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
852b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
853b1b8ab34Slling 	    "PROPERTY"));
854b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
855b1b8ab34Slling 	    "VALUE"));
85692241e0bSTom Erickson 	cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
85792241e0bSTom Erickson 	    "RECEIVED"));
858b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
859b1b8ab34Slling 	    "SOURCE"));
860b1b8ab34Slling 
861deb8317bSMark J Musante 	/* first property is always NAME */
862deb8317bSMark J Musante 	assert(cbp->cb_proplist->pl_prop ==
863deb8317bSMark J Musante 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
864deb8317bSMark J Musante 
865b1b8ab34Slling 	/*
866b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
867b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
868b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
869b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
870b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
87192241e0bSTom Erickson 	 * if the name of the property is much longer than any values we find.
872b1b8ab34Slling 	 */
873b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
874b1b8ab34Slling 		/*
875b1b8ab34Slling 		 * 'PROPERTY' column
876b1b8ab34Slling 		 */
877990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
878990b4856Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
879990b4856Slling 			    zpool_prop_to_name(pl->pl_prop) :
880990b4856Slling 			    zfs_prop_to_name(pl->pl_prop);
881990b4856Slling 
882990b4856Slling 			len = strlen(propname);
883b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
884b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
885b1b8ab34Slling 		} else {
886b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
887b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
888b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
889b1b8ab34Slling 		}
890b1b8ab34Slling 
891b1b8ab34Slling 		/*
892deb8317bSMark J Musante 		 * 'VALUE' column.  The first property is always the 'name'
893deb8317bSMark J Musante 		 * property that was tacked on either by /sbin/zfs's
894deb8317bSMark J Musante 		 * zfs_do_get() or when calling zprop_expand_list(), so we
895deb8317bSMark J Musante 		 * ignore its width.  If the user specified the name property
896deb8317bSMark J Musante 		 * to display, then it will be later in the list in any case.
897b1b8ab34Slling 		 */
898deb8317bSMark J Musante 		if (pl != cbp->cb_proplist &&
899b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
900b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
901b1b8ab34Slling 
90292241e0bSTom Erickson 		/* 'RECEIVED' column. */
90392241e0bSTom Erickson 		if (pl != cbp->cb_proplist &&
90492241e0bSTom Erickson 		    pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
90592241e0bSTom Erickson 			cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
90692241e0bSTom Erickson 
907b1b8ab34Slling 		/*
908b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
909b1b8ab34Slling 		 */
910990b4856Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
911990b4856Slling 		    ZFS_PROP_NAME) &&
912b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
913b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
914b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
915b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
916b1b8ab34Slling 		}
917b1b8ab34Slling 	}
918b1b8ab34Slling 
919b1b8ab34Slling 	/*
920b1b8ab34Slling 	 * Now go through and print the headers.
921b1b8ab34Slling 	 */
92292241e0bSTom Erickson 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
923b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
924b1b8ab34Slling 		case GET_COL_NAME:
925b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
926b1b8ab34Slling 			break;
927b1b8ab34Slling 		case GET_COL_PROPERTY:
928b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
929b1b8ab34Slling 			break;
930b1b8ab34Slling 		case GET_COL_VALUE:
931b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
932b1b8ab34Slling 			break;
93392241e0bSTom Erickson 		case GET_COL_RECVD:
93492241e0bSTom Erickson 			title = dgettext(TEXT_DOMAIN, "RECEIVED");
93592241e0bSTom Erickson 			break;
936b1b8ab34Slling 		case GET_COL_SOURCE:
937b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
938b1b8ab34Slling 			break;
939b1b8ab34Slling 		default:
940b1b8ab34Slling 			title = NULL;
941b1b8ab34Slling 		}
942b1b8ab34Slling 
943b1b8ab34Slling 		if (title != NULL) {
94492241e0bSTom Erickson 			if (i == (ZFS_GET_NCOLS - 1) ||
94592241e0bSTom Erickson 			    cbp->cb_columns[i + 1] == GET_COL_NONE)
946b1b8ab34Slling 				(void) printf("%s", title);
947b1b8ab34Slling 			else
948b1b8ab34Slling 				(void) printf("%-*s  ",
949b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
950b1b8ab34Slling 				    title);
951b1b8ab34Slling 		}
952b1b8ab34Slling 	}
953b1b8ab34Slling 	(void) printf("\n");
954b1b8ab34Slling }
955b1b8ab34Slling 
956b1b8ab34Slling /*
957b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
958b1b8ab34Slling  * structure.
959b1b8ab34Slling  */
960b1b8ab34Slling void
961990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
962990b4856Slling     const char *propname, const char *value, zprop_source_t sourcetype,
96392241e0bSTom Erickson     const char *source, const char *recvd_value)
964b1b8ab34Slling {
965b1b8ab34Slling 	int i;
966b1b8ab34Slling 	const char *str;
967b1b8ab34Slling 	char buf[128];
968b1b8ab34Slling 
969b1b8ab34Slling 	/*
970b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
971b1b8ab34Slling 	 */
972b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
973b1b8ab34Slling 		return;
974b1b8ab34Slling 
975b1b8ab34Slling 	if (cbp->cb_first)
976990b4856Slling 		zprop_print_headers(cbp, cbp->cb_type);
977b1b8ab34Slling 
97892241e0bSTom Erickson 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
979b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
980b1b8ab34Slling 		case GET_COL_NAME:
981b1b8ab34Slling 			str = name;
982b1b8ab34Slling 			break;
983b1b8ab34Slling 
984b1b8ab34Slling 		case GET_COL_PROPERTY:
985b1b8ab34Slling 			str = propname;
986b1b8ab34Slling 			break;
987b1b8ab34Slling 
988b1b8ab34Slling 		case GET_COL_VALUE:
989b1b8ab34Slling 			str = value;
990b1b8ab34Slling 			break;
991b1b8ab34Slling 
992b1b8ab34Slling 		case GET_COL_SOURCE:
993b1b8ab34Slling 			switch (sourcetype) {
994990b4856Slling 			case ZPROP_SRC_NONE:
995b1b8ab34Slling 				str = "-";
996b1b8ab34Slling 				break;
997b1b8ab34Slling 
998990b4856Slling 			case ZPROP_SRC_DEFAULT:
999b1b8ab34Slling 				str = "default";
1000b1b8ab34Slling 				break;
1001b1b8ab34Slling 
1002990b4856Slling 			case ZPROP_SRC_LOCAL:
1003b1b8ab34Slling 				str = "local";
1004b1b8ab34Slling 				break;
1005b1b8ab34Slling 
1006990b4856Slling 			case ZPROP_SRC_TEMPORARY:
1007b1b8ab34Slling 				str = "temporary";
1008b1b8ab34Slling 				break;
1009b1b8ab34Slling 
1010990b4856Slling 			case ZPROP_SRC_INHERITED:
1011b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
1012b1b8ab34Slling 				    "inherited from %s", source);
1013b1b8ab34Slling 				str = buf;
1014b1b8ab34Slling 				break;
101592241e0bSTom Erickson 			case ZPROP_SRC_RECEIVED:
101692241e0bSTom Erickson 				str = "received";
101792241e0bSTom Erickson 				break;
1018b1b8ab34Slling 			}
1019b1b8ab34Slling 			break;
1020b1b8ab34Slling 
102192241e0bSTom Erickson 		case GET_COL_RECVD:
102292241e0bSTom Erickson 			str = (recvd_value == NULL ? "-" : recvd_value);
102392241e0bSTom Erickson 			break;
102492241e0bSTom Erickson 
1025b1b8ab34Slling 		default:
1026b1b8ab34Slling 			continue;
1027b1b8ab34Slling 		}
1028b1b8ab34Slling 
102992241e0bSTom Erickson 		if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1030b1b8ab34Slling 			(void) printf("%s", str);
1031b1b8ab34Slling 		else if (cbp->cb_scripted)
1032b1b8ab34Slling 			(void) printf("%s\t", str);
1033b1b8ab34Slling 		else
1034b1b8ab34Slling 			(void) printf("%-*s  ",
1035b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
1036b1b8ab34Slling 			    str);
1037b1b8ab34Slling 	}
1038b1b8ab34Slling 
1039b1b8ab34Slling 	(void) printf("\n");
1040b1b8ab34Slling }
1041ecd6cf80Smarks 
1042990b4856Slling /*
1043990b4856Slling  * Given a numeric suffix, convert the value into a number of bits that the
1044990b4856Slling  * resulting value must be shifted.
1045990b4856Slling  */
1046990b4856Slling static int
1047990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf)
1048990b4856Slling {
1049990b4856Slling 	const char *ends = "BKMGTPEZ";
1050990b4856Slling 	int i;
1051990b4856Slling 
1052990b4856Slling 	if (buf[0] == '\0')
1053990b4856Slling 		return (0);
1054990b4856Slling 	for (i = 0; i < strlen(ends); i++) {
1055990b4856Slling 		if (toupper(buf[0]) == ends[i])
1056990b4856Slling 			break;
1057990b4856Slling 	}
1058990b4856Slling 	if (i == strlen(ends)) {
1059990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1060990b4856Slling 		    "invalid numeric suffix '%s'"), buf);
1061990b4856Slling 		return (-1);
1062990b4856Slling 	}
1063990b4856Slling 
1064990b4856Slling 	/*
1065990b4856Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
1066990b4856Slling 	 * allow 'BB' - that's just weird.
1067990b4856Slling 	 */
1068990b4856Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1069990b4856Slling 	    toupper(buf[0]) != 'B'))
1070990b4856Slling 		return (10*i);
1071990b4856Slling 
1072990b4856Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1073990b4856Slling 	    "invalid numeric suffix '%s'"), buf);
1074990b4856Slling 	return (-1);
1075990b4856Slling }
1076990b4856Slling 
1077990b4856Slling /*
1078990b4856Slling  * Convert a string of the form '100G' into a real number.  Used when setting
1079990b4856Slling  * properties or creating a volume.  'buf' is used to place an extended error
1080990b4856Slling  * message for the caller to use.
1081990b4856Slling  */
1082ecd6cf80Smarks int
1083990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1084ecd6cf80Smarks {
1085990b4856Slling 	char *end;
1086990b4856Slling 	int shift;
1087ecd6cf80Smarks 
1088990b4856Slling 	*num = 0;
1089990b4856Slling 
1090990b4856Slling 	/* Check to see if this looks like a number.  */
1091990b4856Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1092990b4856Slling 		if (hdl)
1093990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1094990b4856Slling 			    "bad numeric value '%s'"), value);
1095990b4856Slling 		return (-1);
1096ecd6cf80Smarks 	}
1097ecd6cf80Smarks 
1098069f55e2SEric Schrock 	/* Rely on strtoull() to process the numeric portion.  */
1099990b4856Slling 	errno = 0;
1100f67f35c3SEric Schrock 	*num = strtoull(value, &end, 10);
1101990b4856Slling 
1102990b4856Slling 	/*
1103990b4856Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
1104990b4856Slling 	 * in a 64-bit value.
1105990b4856Slling 	 */
1106990b4856Slling 	if (errno == ERANGE) {
1107990b4856Slling 		if (hdl)
1108990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1109990b4856Slling 			    "numeric value is too large"));
1110990b4856Slling 		return (-1);
1111990b4856Slling 	}
1112990b4856Slling 
1113990b4856Slling 	/*
1114990b4856Slling 	 * If we have a decimal value, then do the computation with floating
1115990b4856Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1116990b4856Slling 	 */
1117990b4856Slling 	if (*end == '.') {
1118990b4856Slling 		double fval = strtod(value, &end);
1119990b4856Slling 
1120990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1121990b4856Slling 			return (-1);
1122990b4856Slling 
1123990b4856Slling 		fval *= pow(2, shift);
1124990b4856Slling 
1125990b4856Slling 		if (fval > UINT64_MAX) {
1126990b4856Slling 			if (hdl)
1127990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1128990b4856Slling 				    "numeric value is too large"));
1129990b4856Slling 			return (-1);
1130990b4856Slling 		}
1131990b4856Slling 
1132990b4856Slling 		*num = (uint64_t)fval;
1133990b4856Slling 	} else {
1134990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1135990b4856Slling 			return (-1);
1136990b4856Slling 
1137990b4856Slling 		/* Check for overflow */
1138990b4856Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1139990b4856Slling 			if (hdl)
1140990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1141990b4856Slling 				    "numeric value is too large"));
1142990b4856Slling 			return (-1);
1143990b4856Slling 		}
1144990b4856Slling 
1145990b4856Slling 		*num <<= shift;
1146990b4856Slling 	}
1147990b4856Slling 
1148990b4856Slling 	return (0);
1149990b4856Slling }
1150990b4856Slling 
1151990b4856Slling /*
1152990b4856Slling  * Given a propname=value nvpair to set, parse any numeric properties
1153990b4856Slling  * (index, boolean, etc) if they are specified as strings and add the
1154990b4856Slling  * resulting nvpair to the returned nvlist.
1155990b4856Slling  *
1156990b4856Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1157990b4856Slling  * We want the user to be able to ignore this fact and specify properties
1158990b4856Slling  * as native values (numbers, for example) or as strings (to simplify
1159990b4856Slling  * command line utilities).  This also handles converting index types
1160990b4856Slling  * (compression, checksum, etc) from strings to their on-disk index.
1161990b4856Slling  */
1162990b4856Slling int
1163990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1164990b4856Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1165990b4856Slling     const char *errbuf)
1166990b4856Slling {
1167990b4856Slling 	data_type_t datatype = nvpair_type(elem);
1168990b4856Slling 	zprop_type_t proptype;
1169990b4856Slling 	const char *propname;
1170990b4856Slling 	char *value;
1171990b4856Slling 	boolean_t isnone = B_FALSE;
1172990b4856Slling 
1173990b4856Slling 	if (type == ZFS_TYPE_POOL) {
1174990b4856Slling 		proptype = zpool_prop_get_type(prop);
1175990b4856Slling 		propname = zpool_prop_to_name(prop);
1176990b4856Slling 	} else {
1177990b4856Slling 		proptype = zfs_prop_get_type(prop);
1178990b4856Slling 		propname = zfs_prop_to_name(prop);
1179990b4856Slling 	}
1180990b4856Slling 
1181990b4856Slling 	/*
1182990b4856Slling 	 * Convert any properties to the internal DSL value types.
1183990b4856Slling 	 */
1184990b4856Slling 	*svalp = NULL;
1185990b4856Slling 	*ivalp = 0;
1186990b4856Slling 
1187990b4856Slling 	switch (proptype) {
1188990b4856Slling 	case PROP_TYPE_STRING:
1189990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1190990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1191990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1192990b4856Slling 			goto error;
1193990b4856Slling 		}
1194990b4856Slling 		(void) nvpair_value_string(elem, svalp);
1195990b4856Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1196990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1197990b4856Slling 			    "'%s' is too long"), nvpair_name(elem));
1198990b4856Slling 			goto error;
1199990b4856Slling 		}
1200990b4856Slling 		break;
1201990b4856Slling 
1202990b4856Slling 	case PROP_TYPE_NUMBER:
1203990b4856Slling 		if (datatype == DATA_TYPE_STRING) {
1204990b4856Slling 			(void) nvpair_value_string(elem, &value);
1205990b4856Slling 			if (strcmp(value, "none") == 0) {
1206990b4856Slling 				isnone = B_TRUE;
1207990b4856Slling 			} else if (zfs_nicestrtonum(hdl, value, ivalp)
1208990b4856Slling 			    != 0) {
1209990b4856Slling 				goto error;
1210990b4856Slling 			}
1211990b4856Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1212990b4856Slling 			(void) nvpair_value_uint64(elem, ivalp);
1213990b4856Slling 		} else {
1214990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1215990b4856Slling 			    "'%s' must be a number"), nvpair_name(elem));
1216990b4856Slling 			goto error;
1217990b4856Slling 		}
1218990b4856Slling 
1219990b4856Slling 		/*
1220990b4856Slling 		 * Quota special: force 'none' and don't allow 0.
1221990b4856Slling 		 */
1222a9799022Sck 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1223a9799022Sck 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1224990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225a9799022Sck 			    "use 'none' to disable quota/refquota"));
1226990b4856Slling 			goto error;
1227990b4856Slling 		}
1228a2afb611SJerry Jelinek 
1229a2afb611SJerry Jelinek 		/*
1230a2afb611SJerry Jelinek 		 * Special handling for "*_limit=none". In this case it's not
1231a2afb611SJerry Jelinek 		 * 0 but UINT64_MAX.
1232a2afb611SJerry Jelinek 		 */
1233a2afb611SJerry Jelinek 		if ((type & ZFS_TYPE_DATASET) && isnone &&
1234a2afb611SJerry Jelinek 		    (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1235a2afb611SJerry Jelinek 		    prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1236a2afb611SJerry Jelinek 			*ivalp = UINT64_MAX;
1237a2afb611SJerry Jelinek 		}
1238990b4856Slling 		break;
1239990b4856Slling 
1240990b4856Slling 	case PROP_TYPE_INDEX:
1241a9799022Sck 		if (datatype != DATA_TYPE_STRING) {
1242990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1243990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1244990b4856Slling 			goto error;
1245990b4856Slling 		}
1246990b4856Slling 
1247a9799022Sck 		(void) nvpair_value_string(elem, &value);
1248a9799022Sck 
1249990b4856Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1250990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1251990b4856Slling 			    "'%s' must be one of '%s'"), propname,
1252990b4856Slling 			    zprop_values(prop, type));
1253990b4856Slling 			goto error;
1254990b4856Slling 		}
1255990b4856Slling 		break;
1256990b4856Slling 
1257990b4856Slling 	default:
1258990b4856Slling 		abort();
1259990b4856Slling 	}
1260990b4856Slling 
1261990b4856Slling 	/*
1262990b4856Slling 	 * Add the result to our return set of properties.
1263990b4856Slling 	 */
1264990b4856Slling 	if (*svalp != NULL) {
1265990b4856Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1266990b4856Slling 			(void) no_memory(hdl);
1267990b4856Slling 			return (-1);
1268990b4856Slling 		}
1269990b4856Slling 	} else {
1270990b4856Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1271990b4856Slling 			(void) no_memory(hdl);
1272990b4856Slling 			return (-1);
1273990b4856Slling 		}
1274990b4856Slling 	}
1275990b4856Slling 
1276990b4856Slling 	return (0);
1277990b4856Slling error:
1278990b4856Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1279990b4856Slling 	return (-1);
1280990b4856Slling }
1281990b4856Slling 
128274e7dc98SMatthew Ahrens static int
128374e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
128474e7dc98SMatthew Ahrens     zfs_type_t type)
128574e7dc98SMatthew Ahrens {
128674e7dc98SMatthew Ahrens 	int prop;
128774e7dc98SMatthew Ahrens 	zprop_list_t *entry;
128874e7dc98SMatthew Ahrens 
128974e7dc98SMatthew Ahrens 	prop = zprop_name_to_prop(propname, type);
129074e7dc98SMatthew Ahrens 
129174e7dc98SMatthew Ahrens 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
129274e7dc98SMatthew Ahrens 		prop = ZPROP_INVAL;
129374e7dc98SMatthew Ahrens 
129474e7dc98SMatthew Ahrens 	/*
129574e7dc98SMatthew Ahrens 	 * When no property table entry can be found, return failure if
129674e7dc98SMatthew Ahrens 	 * this is a pool property or if this isn't a user-defined
129774e7dc98SMatthew Ahrens 	 * dataset property,
129874e7dc98SMatthew Ahrens 	 */
1299ad135b5dSChristopher Siden 	if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1300ad135b5dSChristopher Siden 	    !zpool_prop_feature(propname) &&
1301ad135b5dSChristopher Siden 	    !zpool_prop_unsupported(propname)) ||
1302ad135b5dSChristopher Siden 	    (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1303ad135b5dSChristopher Siden 	    !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
130474e7dc98SMatthew Ahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
130574e7dc98SMatthew Ahrens 		    "invalid property '%s'"), propname);
130674e7dc98SMatthew Ahrens 		return (zfs_error(hdl, EZFS_BADPROP,
130774e7dc98SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "bad property list")));
130874e7dc98SMatthew Ahrens 	}
130974e7dc98SMatthew Ahrens 
131074e7dc98SMatthew Ahrens 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
131174e7dc98SMatthew Ahrens 		return (-1);
131274e7dc98SMatthew Ahrens 
131374e7dc98SMatthew Ahrens 	entry->pl_prop = prop;
131474e7dc98SMatthew Ahrens 	if (prop == ZPROP_INVAL) {
1315ad135b5dSChristopher Siden 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1316ad135b5dSChristopher Siden 		    NULL) {
131774e7dc98SMatthew Ahrens 			free(entry);
131874e7dc98SMatthew Ahrens 			return (-1);
131974e7dc98SMatthew Ahrens 		}
132074e7dc98SMatthew Ahrens 		entry->pl_width = strlen(propname);
132174e7dc98SMatthew Ahrens 	} else {
132274e7dc98SMatthew Ahrens 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
132374e7dc98SMatthew Ahrens 		    type);
132474e7dc98SMatthew Ahrens 	}
132574e7dc98SMatthew Ahrens 
132674e7dc98SMatthew Ahrens 	*listp = entry;
132774e7dc98SMatthew Ahrens 
132874e7dc98SMatthew Ahrens 	return (0);
132974e7dc98SMatthew Ahrens }
133074e7dc98SMatthew Ahrens 
1331990b4856Slling /*
1332990b4856Slling  * Given a comma-separated list of properties, construct a property list
1333990b4856Slling  * containing both user-defined and native properties.  This function will
1334990b4856Slling  * return a NULL list if 'all' is specified, which can later be expanded
1335990b4856Slling  * by zprop_expand_list().
1336990b4856Slling  */
1337990b4856Slling int
1338990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1339990b4856Slling     zfs_type_t type)
1340990b4856Slling {
1341990b4856Slling 	*listp = NULL;
1342990b4856Slling 
1343990b4856Slling 	/*
1344990b4856Slling 	 * If 'all' is specified, return a NULL list.
1345990b4856Slling 	 */
1346990b4856Slling 	if (strcmp(props, "all") == 0)
1347990b4856Slling 		return (0);
1348990b4856Slling 
1349990b4856Slling 	/*
1350990b4856Slling 	 * If no props were specified, return an error.
1351990b4856Slling 	 */
1352990b4856Slling 	if (props[0] == '\0') {
1353990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1354990b4856Slling 		    "no properties specified"));
1355990b4856Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1356990b4856Slling 		    "bad property list")));
1357990b4856Slling 	}
1358990b4856Slling 
1359990b4856Slling 	/*
1360990b4856Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1361990b4856Slling 	 * aliases makes this more effort than it's worth.
1362990b4856Slling 	 */
136374e7dc98SMatthew Ahrens 	while (*props != '\0') {
136474e7dc98SMatthew Ahrens 		size_t len;
136574e7dc98SMatthew Ahrens 		char *p;
136674e7dc98SMatthew Ahrens 		char c;
136774e7dc98SMatthew Ahrens 
136874e7dc98SMatthew Ahrens 		if ((p = strchr(props, ',')) == NULL) {
136974e7dc98SMatthew Ahrens 			len = strlen(props);
137074e7dc98SMatthew Ahrens 			p = props + len;
1371990b4856Slling 		} else {
137274e7dc98SMatthew Ahrens 			len = p - props;
1373990b4856Slling 		}
1374990b4856Slling 
1375990b4856Slling 		/*
1376990b4856Slling 		 * Check for empty options.
1377990b4856Slling 		 */
1378990b4856Slling 		if (len == 0) {
1379990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1380990b4856Slling 			    "empty property name"));
1381990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1382990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1383990b4856Slling 		}
1384990b4856Slling 
1385990b4856Slling 		/*
1386990b4856Slling 		 * Check all regular property names.
1387990b4856Slling 		 */
138874e7dc98SMatthew Ahrens 		c = props[len];
138974e7dc98SMatthew Ahrens 		props[len] = '\0';
139074e7dc98SMatthew Ahrens 
139174e7dc98SMatthew Ahrens 		if (strcmp(props, "space") == 0) {
139274e7dc98SMatthew Ahrens 			static char *spaceprops[] = {
139374e7dc98SMatthew Ahrens 				"name", "avail", "used", "usedbysnapshots",
139474e7dc98SMatthew Ahrens 				"usedbydataset", "usedbyrefreservation",
139574e7dc98SMatthew Ahrens 				"usedbychildren", NULL
139674e7dc98SMatthew Ahrens 			};
139774e7dc98SMatthew Ahrens 			int i;
139874e7dc98SMatthew Ahrens 
139974e7dc98SMatthew Ahrens 			for (i = 0; spaceprops[i]; i++) {
140074e7dc98SMatthew Ahrens 				if (addlist(hdl, spaceprops[i], listp, type))
140174e7dc98SMatthew Ahrens 					return (-1);
140274e7dc98SMatthew Ahrens 				listp = &(*listp)->pl_next;
1403990b4856Slling 			}
1404990b4856Slling 		} else {
140574e7dc98SMatthew Ahrens 			if (addlist(hdl, props, listp, type))
140674e7dc98SMatthew Ahrens 				return (-1);
140774e7dc98SMatthew Ahrens 			listp = &(*listp)->pl_next;
1408990b4856Slling 		}
1409990b4856Slling 
141074e7dc98SMatthew Ahrens 		props = p;
1411990b4856Slling 		if (c == ',')
141274e7dc98SMatthew Ahrens 			props++;
1413990b4856Slling 	}
1414990b4856Slling 
1415990b4856Slling 	return (0);
1416990b4856Slling }
1417990b4856Slling 
1418990b4856Slling void
1419990b4856Slling zprop_free_list(zprop_list_t *pl)
1420990b4856Slling {
1421990b4856Slling 	zprop_list_t *next;
1422990b4856Slling 
1423990b4856Slling 	while (pl != NULL) {
1424990b4856Slling 		next = pl->pl_next;
1425990b4856Slling 		free(pl->pl_user_prop);
1426990b4856Slling 		free(pl);
1427990b4856Slling 		pl = next;
1428990b4856Slling 	}
1429990b4856Slling }
1430990b4856Slling 
1431990b4856Slling typedef struct expand_data {
1432990b4856Slling 	zprop_list_t	**last;
1433990b4856Slling 	libzfs_handle_t	*hdl;
1434990b4856Slling 	zfs_type_t type;
1435990b4856Slling } expand_data_t;
1436990b4856Slling 
1437990b4856Slling int
1438990b4856Slling zprop_expand_list_cb(int prop, void *cb)
1439990b4856Slling {
1440990b4856Slling 	zprop_list_t *entry;
1441990b4856Slling 	expand_data_t *edp = cb;
1442990b4856Slling 
1443990b4856Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1444990b4856Slling 		return (ZPROP_INVAL);
1445990b4856Slling 
1446990b4856Slling 	entry->pl_prop = prop;
1447990b4856Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1448990b4856Slling 	entry->pl_all = B_TRUE;
1449990b4856Slling 
1450990b4856Slling 	*(edp->last) = entry;
1451990b4856Slling 	edp->last = &entry->pl_next;
1452990b4856Slling 
1453990b4856Slling 	return (ZPROP_CONT);
1454990b4856Slling }
1455990b4856Slling 
1456990b4856Slling int
1457990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1458990b4856Slling {
1459990b4856Slling 	zprop_list_t *entry;
1460990b4856Slling 	zprop_list_t **last;
1461990b4856Slling 	expand_data_t exp;
1462990b4856Slling 
1463990b4856Slling 	if (*plp == NULL) {
1464990b4856Slling 		/*
1465990b4856Slling 		 * If this is the very first time we've been called for an 'all'
1466990b4856Slling 		 * specification, expand the list to include all native
1467990b4856Slling 		 * properties.
1468990b4856Slling 		 */
1469990b4856Slling 		last = plp;
1470990b4856Slling 
1471990b4856Slling 		exp.last = last;
1472990b4856Slling 		exp.hdl = hdl;
1473990b4856Slling 		exp.type = type;
1474990b4856Slling 
1475990b4856Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1476990b4856Slling 		    B_FALSE, type) == ZPROP_INVAL)
1477990b4856Slling 			return (-1);
1478990b4856Slling 
1479990b4856Slling 		/*
1480990b4856Slling 		 * Add 'name' to the beginning of the list, which is handled
1481990b4856Slling 		 * specially.
1482990b4856Slling 		 */
1483990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1484990b4856Slling 			return (-1);
1485990b4856Slling 
1486990b4856Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1487990b4856Slling 		    ZFS_PROP_NAME;
1488990b4856Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1489990b4856Slling 		    &entry->pl_fixed, type);
1490990b4856Slling 		entry->pl_all = B_TRUE;
1491990b4856Slling 		entry->pl_next = *plp;
1492990b4856Slling 		*plp = entry;
1493990b4856Slling 	}
1494990b4856Slling 	return (0);
1495990b4856Slling }
1496990b4856Slling 
1497990b4856Slling int
1498990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1499990b4856Slling     zfs_type_t type)
1500990b4856Slling {
1501990b4856Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1502ecd6cf80Smarks }
1503*2bcf0248SMax Grossman 
1504*2bcf0248SMax Grossman /*
1505*2bcf0248SMax Grossman  * zfs_get_hole_count retrieves the number of holes (blocks which are
1506*2bcf0248SMax Grossman  * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It
1507*2bcf0248SMax Grossman  * also optionally fetches the block size when bs is non-NULL. With hole count
1508*2bcf0248SMax Grossman  * and block size the full space consumed by the holes of a file can be
1509*2bcf0248SMax Grossman  * calculated.
1510*2bcf0248SMax Grossman  *
1511*2bcf0248SMax Grossman  * On success, zero is returned, the count argument is set to the
1512*2bcf0248SMax Grossman  * number of holes, and the bs argument is set to the block size (if it is
1513*2bcf0248SMax Grossman  * not NULL). On error, a non-zero errno is returned and the values in count
1514*2bcf0248SMax Grossman  * and bs are undefined.
1515*2bcf0248SMax Grossman  */
1516*2bcf0248SMax Grossman int
1517*2bcf0248SMax Grossman zfs_get_hole_count(const char *path, uint64_t *count, uint64_t *bs) {
1518*2bcf0248SMax Grossman 	int fd, err;
1519*2bcf0248SMax Grossman 	struct stat64 ss;
1520*2bcf0248SMax Grossman 	uint64_t fill;
1521*2bcf0248SMax Grossman 
1522*2bcf0248SMax Grossman 	fd = open(path, O_RDONLY | O_LARGEFILE);
1523*2bcf0248SMax Grossman 	if (fd == -1)
1524*2bcf0248SMax Grossman 		return (errno);
1525*2bcf0248SMax Grossman 
1526*2bcf0248SMax Grossman 	if (ioctl(fd, _FIO_COUNT_FILLED, &fill) == -1) {
1527*2bcf0248SMax Grossman 		err = errno;
1528*2bcf0248SMax Grossman 		(void) close(fd);
1529*2bcf0248SMax Grossman 		return (err);
1530*2bcf0248SMax Grossman 	}
1531*2bcf0248SMax Grossman 
1532*2bcf0248SMax Grossman 	if (fstat64(fd, &ss) == -1) {
1533*2bcf0248SMax Grossman 		err = errno;
1534*2bcf0248SMax Grossman 		(void) close(fd);
1535*2bcf0248SMax Grossman 		return (err);
1536*2bcf0248SMax Grossman 	}
1537*2bcf0248SMax Grossman 
1538*2bcf0248SMax Grossman 	*count = (ss.st_size + ss.st_blksize - 1) / ss.st_blksize - fill;
1539*2bcf0248SMax Grossman 	VERIFY3S(*count, >=, 0);
1540*2bcf0248SMax Grossman 	if (bs != NULL) {
1541*2bcf0248SMax Grossman 		*bs = ss.st_blksize;
1542*2bcf0248SMax Grossman 	}
1543*2bcf0248SMax Grossman 
1544*2bcf0248SMax Grossman 	if (close(fd) == -1) {
1545*2bcf0248SMax Grossman 		return (errno);
1546*2bcf0248SMax Grossman 	}
1547*2bcf0248SMax Grossman 	return (0);
1548*2bcf0248SMax Grossman }
1549