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.
24d8ab6e12SDon Brady  * Copyright 2020 Joyent, Inc.
257ac89354SDon Brady  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
2688f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
271702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens /*
31fa9e4066Sahrens  * Internal utility routines for the ZFS library.
32fa9e4066Sahrens  */
33fa9e4066Sahrens 
34d8ab6e12SDon Brady #include <ctype.h>
35fa9e4066Sahrens #include <errno.h>
36fa9e4066Sahrens #include <fcntl.h>
37fa9e4066Sahrens #include <libintl.h>
38fa9e4066Sahrens #include <stdarg.h>
39fa9e4066Sahrens #include <stdio.h>
40fa9e4066Sahrens #include <stdlib.h>
41fa9e4066Sahrens #include <strings.h>
42990b4856Slling #include <math.h>
432bcf0248SMax Grossman #include <sys/filio.h>
44fa9e4066Sahrens #include <sys/mnttab.h>
455aba80dbSck #include <sys/mntent.h>
465aba80dbSck #include <sys/types.h>
470a055120SJason King #include <libcmdutils.h>
48fa9e4066Sahrens 
49fa9e4066Sahrens #include <libzfs.h>
504445fffbSMatthew Ahrens #include <libzfs_core.h>
51fa9e4066Sahrens 
52fa9e4066Sahrens #include "libzfs_impl.h"
5391ebeef5Sahrens #include "zfs_prop.h"
54e0f1c0afSOlaf Faaland #include "zfs_comutil.h"
55ad135b5dSChristopher Siden #include "zfeature_common.h"
56*0886dcadSAndy Fiddaman #include <zfs_fletcher.h>
57d8ab6e12SDon Brady #include <libzutil.h>
58fa9e4066Sahrens 
5999653d4eSeschrock int
libzfs_errno(libzfs_handle_t * hdl)6099653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
6199653d4eSeschrock {
6299653d4eSeschrock 	return (hdl->libzfs_error);
6399653d4eSeschrock }
64fa9e4066Sahrens 
6599653d4eSeschrock const char *
libzfs_error_action(libzfs_handle_t * hdl)6699653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
6799653d4eSeschrock {
6899653d4eSeschrock 	return (hdl->libzfs_action);
6999653d4eSeschrock }
70fa9e4066Sahrens 
7199653d4eSeschrock const char *
libzfs_error_description(libzfs_handle_t * hdl)7299653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl)
7399653d4eSeschrock {
7499653d4eSeschrock 	if (hdl->libzfs_desc[0] != '\0')
7599653d4eSeschrock 		return (hdl->libzfs_desc);
7699653d4eSeschrock 
7799653d4eSeschrock 	switch (hdl->libzfs_error) {
7899653d4eSeschrock 	case EZFS_NOMEM:
7999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
8099653d4eSeschrock 	case EZFS_BADPROP:
8199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
8299653d4eSeschrock 	case EZFS_PROPREADONLY:
83f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "read-only property"));
8499653d4eSeschrock 	case EZFS_PROPTYPE:
8599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
8699653d4eSeschrock 		    "datasets of this type"));
8799653d4eSeschrock 	case EZFS_PROPNONINHERIT:
8899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
8999653d4eSeschrock 	case EZFS_PROPSPACE:
9099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
9199653d4eSeschrock 	case EZFS_BADTYPE:
9299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
9399653d4eSeschrock 		    "datasets of this type"));
9499653d4eSeschrock 	case EZFS_BUSY:
9599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
9699653d4eSeschrock 	case EZFS_EXISTS:
9799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
9899653d4eSeschrock 	case EZFS_NOENT:
9999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
10099653d4eSeschrock 	case EZFS_BADSTREAM:
10199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
10299653d4eSeschrock 	case EZFS_DSREADONLY:
103f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
10499653d4eSeschrock 	case EZFS_VOLTOOBIG:
10599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
10699653d4eSeschrock 		    "this system"));
10799653d4eSeschrock 	case EZFS_INVALIDNAME:
10899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
10999653d4eSeschrock 	case EZFS_BADRESTORE:
11099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
11199653d4eSeschrock 		    "destination"));
11299653d4eSeschrock 	case EZFS_BADBACKUP:
11399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
11499653d4eSeschrock 	case EZFS_BADTARGET:
11599653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
11699653d4eSeschrock 	case EZFS_NODEVICE:
11799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
11899653d4eSeschrock 	case EZFS_BADDEV:
11999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
12099653d4eSeschrock 	case EZFS_NOREPLICAS:
12199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
12299653d4eSeschrock 	case EZFS_RESILVERING:
12399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
12499653d4eSeschrock 	case EZFS_BADVERSION:
125ad135b5dSChristopher Siden 		return (dgettext(TEXT_DOMAIN, "unsupported version or "
126ad135b5dSChristopher Siden 		    "feature"));
12799653d4eSeschrock 	case EZFS_POOLUNAVAIL:
12899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
12999653d4eSeschrock 	case EZFS_DEVOVERFLOW:
13099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
13199653d4eSeschrock 	case EZFS_BADPATH:
13299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
13399653d4eSeschrock 	case EZFS_CROSSTARGET:
13499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
13599653d4eSeschrock 		    "pools"));
13699653d4eSeschrock 	case EZFS_ZONED:
13799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
13899653d4eSeschrock 	case EZFS_MOUNTFAILED:
13999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
14099653d4eSeschrock 	case EZFS_UMOUNTFAILED:
14199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
142f3861e1aSahl 	case EZFS_UNSHARENFSFAILED:
143bbf21555SRichard Lowe 		return (dgettext(TEXT_DOMAIN, "unshare(8) failed"));
144f3861e1aSahl 	case EZFS_SHARENFSFAILED:
145bbf21555SRichard Lowe 		return (dgettext(TEXT_DOMAIN, "share(8) failed"));
146da6c28aaSamw 	case EZFS_UNSHARESMBFAILED:
147da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
148da6c28aaSamw 	case EZFS_SHARESMBFAILED:
149da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
15099653d4eSeschrock 	case EZFS_PERM:
15199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
15299653d4eSeschrock 	case EZFS_NOSPC:
15399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
1546e27f868SSam Falkner 	case EZFS_FAULT:
1556e27f868SSam Falkner 		return (dgettext(TEXT_DOMAIN, "bad address"));
15699653d4eSeschrock 	case EZFS_IO:
15799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
15899653d4eSeschrock 	case EZFS_INTR:
15999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
16099653d4eSeschrock 	case EZFS_ISSPARE:
16199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
16299653d4eSeschrock 		    "spare"));
16399653d4eSeschrock 	case EZFS_INVALCONFIG:
16499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1653bb79becSeschrock 	case EZFS_RECURSIVE:
1663bb79becSeschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
16706eeb2adSek 	case EZFS_NOHISTORY:
16806eeb2adSek 		return (dgettext(TEXT_DOMAIN, "no history available"));
169b1b8ab34Slling 	case EZFS_POOLPROPS:
170b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
171b1b8ab34Slling 		    "pool properties"));
172b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
173b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
174b1b8ab34Slling 		    "on this type of pool"));
175b1b8ab34Slling 	case EZFS_POOL_INVALARG:
176b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
177b1b8ab34Slling 		    "this pool operation"));
178b7661cccSmmusante 	case EZFS_NAMETOOLONG:
179b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1808488aeb5Staylor 	case EZFS_OPENFAILED:
1818488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1828488aeb5Staylor 	case EZFS_NOCAP:
1838488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1848488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1858488aeb5Staylor 	case EZFS_LABELFAILED:
1868488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
187ecd6cf80Smarks 	case EZFS_BADWHO:
188ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
189ecd6cf80Smarks 	case EZFS_BADPERM:
190ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
191ecd6cf80Smarks 	case EZFS_BADPERMSET:
192ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
193ecd6cf80Smarks 	case EZFS_NODELEGATION:
194ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
195ecd6cf80Smarks 		    "disabled on pool"));
1962f8aaab3Seschrock 	case EZFS_BADCACHE:
1972f8aaab3Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
198fa94a07fSbrendan 	case EZFS_ISL2CACHE:
199fa94a07fSbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
200e7cbe64fSgw 	case EZFS_VDEVNOTSUP:
201e7cbe64fSgw 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
202e7cbe64fSgw 		    "supported"));
20315e6edf1Sgw 	case EZFS_NOTSUP:
20415e6edf1Sgw 		return (dgettext(TEXT_DOMAIN, "operation not supported "
20515e6edf1Sgw 		    "on this dataset"));
2067ac89354SDon Brady 	case EZFS_IOC_NOTSUPPORTED:
2077ac89354SDon Brady 		return (dgettext(TEXT_DOMAIN, "operation not supported by "
2087ac89354SDon Brady 		    "zfs kernel module"));
20989a89ebfSlling 	case EZFS_ACTIVE_SPARE:
21089a89ebfSlling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
21189a89ebfSlling 		    "device"));
212e6ca193dSGeorge Wilson 	case EZFS_UNPLAYED_LOGS:
213e6ca193dSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
214e6ca193dSGeorge Wilson 		    "logs"));
215842727c2SChris Kirby 	case EZFS_REFTAG_RELE:
216842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
217842727c2SChris Kirby 	case EZFS_REFTAG_HOLD:
218842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
219842727c2SChris Kirby 		    "dataset"));
220ca45db41SChris Kirby 	case EZFS_TAGTOOLONG:
221ca45db41SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag too long"));
2229e69d7d0SLori Alt 	case EZFS_PIPEFAILED:
2239e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
2249e69d7d0SLori Alt 	case EZFS_THREADCREATEFAILED:
2259e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "thread create failed"));
2261195e687SMark J Musante 	case EZFS_POSTSPLIT_ONLINE:
2271195e687SMark J Musante 		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
2281195e687SMark J Musante 		    "into a new one"));
2291702cce7SAlek Pinchuk 	case EZFS_SCRUB_PAUSED:
2301702cce7SAlek Pinchuk 		return (dgettext(TEXT_DOMAIN, "scrub is paused; "
2311702cce7SAlek Pinchuk 		    "use 'zpool scrub' to resume"));
2323f9d6ad7SLin Ling 	case EZFS_SCRUBBING:
2333f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
2343f9d6ad7SLin Ling 		    "use 'zpool scrub -s' to cancel current scrub"));
2353f9d6ad7SLin Ling 	case EZFS_NO_SCRUB:
2363f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
23799d5e173STim Haley 	case EZFS_DIFF:
23899d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
23999d5e173STim Haley 	case EZFS_DIFFDATA:
24099d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
241f9af39baSGeorge Wilson 	case EZFS_POOLREADONLY:
242f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
2435cabbc6bSPrashanth Sreenivasa 	case EZFS_NO_PENDING:
2445cabbc6bSPrashanth Sreenivasa 		return (dgettext(TEXT_DOMAIN, "operation is not "
2455cabbc6bSPrashanth Sreenivasa 		    "in progress"));
24686714001SSerapheim Dimitropoulos 	case EZFS_CHECKPOINT_EXISTS:
24786714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
24886714001SSerapheim Dimitropoulos 	case EZFS_DISCARDING_CHECKPOINT:
24986714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "currently discarding "
25086714001SSerapheim Dimitropoulos 		    "checkpoint"));
25186714001SSerapheim Dimitropoulos 	case EZFS_NO_CHECKPOINT:
25286714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
25386714001SSerapheim Dimitropoulos 	case EZFS_DEVRM_IN_PROGRESS:
25486714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "device removal in progress"));
25586714001SSerapheim Dimitropoulos 	case EZFS_VDEV_TOO_BIG:
25686714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
257e0f1c0afSOlaf Faaland 	case EZFS_ACTIVE_POOL:
258e0f1c0afSOlaf Faaland 		return (dgettext(TEXT_DOMAIN, "pool is imported on a "
259e0f1c0afSOlaf Faaland 		    "different host"));
260084fd14fSBrian Behlendorf 	case EZFS_CRYPTOFAILED:
261084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "encryption failure"));
262094e47e9SGeorge Wilson 	case EZFS_TOOMANY:
263094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "argument list too long"));
264094e47e9SGeorge Wilson 	case EZFS_INITIALIZING:
265094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "currently initializing"));
266094e47e9SGeorge Wilson 	case EZFS_NO_INITIALIZE:
267094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "there is no active "
268094e47e9SGeorge Wilson 		    "initialization"));
2696ccda740Sloli 	case EZFS_WRONG_PARENT:
2706ccda740Sloli 		return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
271084fd14fSBrian Behlendorf 	case EZFS_TRIMMING:
272084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "currently trimming"));
273084fd14fSBrian Behlendorf 	case EZFS_NO_TRIM:
274084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "there is no active trim"));
275084fd14fSBrian Behlendorf 	case EZFS_TRIM_NOTSUP:
276084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "trim operations are not "
277084fd14fSBrian Behlendorf 		    "supported by this device"));
278e4c795beSTom Caputi 	case EZFS_NO_RESILVER_DEFER:
279e4c795beSTom Caputi 		return (dgettext(TEXT_DOMAIN, "this action requires the "
280e4c795beSTom Caputi 		    "resilver_defer feature"));
28199653d4eSeschrock 	case EZFS_UNKNOWN:
28299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
28399653d4eSeschrock 	default:
284c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
285c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
28699653d4eSeschrock 	}
28799653d4eSeschrock }
28899653d4eSeschrock 
28999653d4eSeschrock /*PRINTFLIKE2*/
290fa9e4066Sahrens void
zfs_error_aux(libzfs_handle_t * hdl,const char * fmt,...)29199653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
292fa9e4066Sahrens {
293fa9e4066Sahrens 	va_list ap;
294fa9e4066Sahrens 
295fa9e4066Sahrens 	va_start(ap, fmt);
296fa9e4066Sahrens 
29799653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
29899653d4eSeschrock 	    fmt, ap);
29999653d4eSeschrock 	hdl->libzfs_desc_active = 1;
30099653d4eSeschrock 
30199653d4eSeschrock 	va_end(ap);
30299653d4eSeschrock }
30399653d4eSeschrock 
30499653d4eSeschrock static void
zfs_verror(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)30599653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
30699653d4eSeschrock {
30799653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
30899653d4eSeschrock 	    fmt, ap);
30999653d4eSeschrock 	hdl->libzfs_error = error;
31099653d4eSeschrock 
31199653d4eSeschrock 	if (hdl->libzfs_desc_active)
31299653d4eSeschrock 		hdl->libzfs_desc_active = 0;
31399653d4eSeschrock 	else
31499653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
31599653d4eSeschrock 
31699653d4eSeschrock 	if (hdl->libzfs_printerr) {
31799653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
31899653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
31999653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
32099653d4eSeschrock 			abort();
32199653d4eSeschrock 		}
32299653d4eSeschrock 
32399653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
324b1b8ab34Slling 		    libzfs_error_description(hdl));
32599653d4eSeschrock 		if (error == EZFS_NOMEM)
32699653d4eSeschrock 			exit(1);
327fa9e4066Sahrens 	}
32899653d4eSeschrock }
32999653d4eSeschrock 
330ece3d9b3Slling int
zfs_error(libzfs_handle_t * hdl,int error,const char * msg)331ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
332ece3d9b3Slling {
333ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
334ece3d9b3Slling }
335ece3d9b3Slling 
33699653d4eSeschrock /*PRINTFLIKE3*/
33799653d4eSeschrock int
zfs_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)338ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
33999653d4eSeschrock {
34099653d4eSeschrock 	va_list ap;
34199653d4eSeschrock 
34299653d4eSeschrock 	va_start(ap, fmt);
34399653d4eSeschrock 
34499653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
345fa9e4066Sahrens 
346fa9e4066Sahrens 	va_end(ap);
34799653d4eSeschrock 
34899653d4eSeschrock 	return (-1);
349fa9e4066Sahrens }
350fa9e4066Sahrens 
35199653d4eSeschrock static int
zfs_common_error(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)35299653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
35399653d4eSeschrock     va_list ap)
35499653d4eSeschrock {
35599653d4eSeschrock 	switch (error) {
35699653d4eSeschrock 	case EPERM:
35799653d4eSeschrock 	case EACCES:
35899653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
35999653d4eSeschrock 		return (-1);
36099653d4eSeschrock 
361ecd6cf80Smarks 	case ECANCELED:
362ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
363ecd6cf80Smarks 		return (-1);
364ecd6cf80Smarks 
36599653d4eSeschrock 	case EIO:
36699653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
36799653d4eSeschrock 		return (-1);
36899653d4eSeschrock 
3696e27f868SSam Falkner 	case EFAULT:
3706e27f868SSam Falkner 		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
3716e27f868SSam Falkner 		return (-1);
3726e27f868SSam Falkner 
37399653d4eSeschrock 	case EINTR:
37499653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
37599653d4eSeschrock 		return (-1);
37699653d4eSeschrock 	}
37799653d4eSeschrock 
37899653d4eSeschrock 	return (0);
37999653d4eSeschrock }
38099653d4eSeschrock 
381ece3d9b3Slling int
zfs_standard_error(libzfs_handle_t * hdl,int error,const char * msg)382ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
383ece3d9b3Slling {
384ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
385ece3d9b3Slling }
386ece3d9b3Slling 
38799653d4eSeschrock /*PRINTFLIKE3*/
38899653d4eSeschrock int
zfs_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)389ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
390fa9e4066Sahrens {
391fa9e4066Sahrens 	va_list ap;
392fa9e4066Sahrens 
393fa9e4066Sahrens 	va_start(ap, fmt);
394fa9e4066Sahrens 
39599653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
39699653d4eSeschrock 		va_end(ap);
39799653d4eSeschrock 		return (-1);
398fa9e4066Sahrens 	}
399fa9e4066Sahrens 
40099653d4eSeschrock 	switch (error) {
40199653d4eSeschrock 	case ENXIO:
40297d9e3a6Sck 	case ENODEV:
40319b94df9SMatthew Ahrens 	case EPIPE:
40499653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
40599653d4eSeschrock 		break;
40699653d4eSeschrock 
40799653d4eSeschrock 	case ENOENT:
40899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40999653d4eSeschrock 		    "dataset does not exist"));
41099653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
41199653d4eSeschrock 		break;
41299653d4eSeschrock 
41399653d4eSeschrock 	case ENOSPC:
41499653d4eSeschrock 	case EDQUOT:
41599653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
41699653d4eSeschrock 		return (-1);
41799653d4eSeschrock 
41899653d4eSeschrock 	case EEXIST:
41999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42099653d4eSeschrock 		    "dataset already exists"));
42199653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
42299653d4eSeschrock 		break;
42399653d4eSeschrock 
42499653d4eSeschrock 	case EBUSY:
42599653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42699653d4eSeschrock 		    "dataset is busy"));
42799653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
42899653d4eSeschrock 		break;
429ecd6cf80Smarks 	case EROFS:
430f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
431ecd6cf80Smarks 		break;
432b7661cccSmmusante 	case ENAMETOOLONG:
433b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
434b7661cccSmmusante 		break;
43540ff3960Sck 	case ENOTSUP:
43640ff3960Sck 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
43740ff3960Sck 		break;
43854d692b7SGeorge Wilson 	case EAGAIN:
43954d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44054d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
44154d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
44254d692b7SGeorge Wilson 		break;
443e0f1c0afSOlaf Faaland 	case EREMOTEIO:
444e0f1c0afSOlaf Faaland 		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
445e0f1c0afSOlaf Faaland 		break;
4467ac89354SDon Brady 	case ZFS_ERR_IOC_CMD_UNAVAIL:
4477ac89354SDon Brady 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
4487ac89354SDon Brady 		    "module does not support this operation. A reboot may "
4497ac89354SDon Brady 		    "be required to enable this operation."));
4507ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
4517ac89354SDon Brady 		break;
4527ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_UNAVAIL:
4537ac89354SDon Brady 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
4547ac89354SDon Brady 		    "module does not support an option for this operation. "
4557ac89354SDon Brady 		    "A reboot may be required to enable this option."));
4567ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
4577ac89354SDon Brady 		break;
4587ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_REQUIRED:
4597ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_BADTYPE:
4607ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
4617ac89354SDon Brady 		break;
46299653d4eSeschrock 	default:
46392241e0bSTom Erickson 		zfs_error_aux(hdl, strerror(error));
46499653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
46599653d4eSeschrock 		break;
46699653d4eSeschrock 	}
46799653d4eSeschrock 
46899653d4eSeschrock 	va_end(ap);
46999653d4eSeschrock 	return (-1);
470fa9e4066Sahrens }
471fa9e4066Sahrens 
472ece3d9b3Slling int
zpool_standard_error(libzfs_handle_t * hdl,int error,const char * msg)473ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
474ece3d9b3Slling {
475ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
476ece3d9b3Slling }
477ece3d9b3Slling 
47899653d4eSeschrock /*PRINTFLIKE3*/
47999653d4eSeschrock int
zpool_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)480ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
481fa9e4066Sahrens {
48299653d4eSeschrock 	va_list ap;
48399653d4eSeschrock 
48499653d4eSeschrock 	va_start(ap, fmt);
48599653d4eSeschrock 
48699653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
48799653d4eSeschrock 		va_end(ap);
48899653d4eSeschrock 		return (-1);
48999653d4eSeschrock 	}
49099653d4eSeschrock 
49199653d4eSeschrock 	switch (error) {
49299653d4eSeschrock 	case ENODEV:
49399653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
49499653d4eSeschrock 		break;
49599653d4eSeschrock 
49699653d4eSeschrock 	case ENOENT:
497b1b8ab34Slling 		zfs_error_aux(hdl,
498b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
49999653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
50099653d4eSeschrock 		break;
50199653d4eSeschrock 
50299653d4eSeschrock 	case EEXIST:
50399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50499653d4eSeschrock 		    "pool already exists"));
50599653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
50699653d4eSeschrock 		break;
50799653d4eSeschrock 
50899653d4eSeschrock 	case EBUSY:
50999653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
51043afaaa8SEric Schrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
51199653d4eSeschrock 		break;
51299653d4eSeschrock 
51399653d4eSeschrock 	case ENXIO:
51499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51599653d4eSeschrock 		    "one or more devices is currently unavailable"));
51699653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
51799653d4eSeschrock 		break;
51899653d4eSeschrock 
51999653d4eSeschrock 	case ENAMETOOLONG:
52099653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
52199653d4eSeschrock 		break;
52299653d4eSeschrock 
523b1b8ab34Slling 	case ENOTSUP:
524b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
525b1b8ab34Slling 		break;
526b1b8ab34Slling 
527b1b8ab34Slling 	case EINVAL:
528b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
529b1b8ab34Slling 		break;
530b1b8ab34Slling 
531ecd6cf80Smarks 	case ENOSPC:
532ecd6cf80Smarks 	case EDQUOT:
533ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
534ecd6cf80Smarks 		return (-1);
535f9af39baSGeorge Wilson 
53654d692b7SGeorge Wilson 	case EAGAIN:
53754d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
53854d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
53954d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
54054d692b7SGeorge Wilson 		break;
541ecd6cf80Smarks 
542f9af39baSGeorge Wilson 	case EROFS:
543f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
544f9af39baSGeorge Wilson 		break;
5455cabbc6bSPrashanth Sreenivasa 	/* There is no pending operation to cancel */
5465cabbc6bSPrashanth Sreenivasa 	case ENOTACTIVE:
5475cabbc6bSPrashanth Sreenivasa 		zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
5485cabbc6bSPrashanth Sreenivasa 		break;
549e0f1c0afSOlaf Faaland 	case EREMOTEIO:
550e0f1c0afSOlaf Faaland 		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
551e0f1c0afSOlaf Faaland 		break;
55286714001SSerapheim Dimitropoulos 	case ZFS_ERR_CHECKPOINT_EXISTS:
55386714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
55486714001SSerapheim Dimitropoulos 		break;
55586714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
55686714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
55786714001SSerapheim Dimitropoulos 		break;
55886714001SSerapheim Dimitropoulos 	case ZFS_ERR_NO_CHECKPOINT:
55986714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
56086714001SSerapheim Dimitropoulos 		break;
56186714001SSerapheim Dimitropoulos 	case ZFS_ERR_DEVRM_IN_PROGRESS:
56286714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
56386714001SSerapheim Dimitropoulos 		break;
56486714001SSerapheim Dimitropoulos 	case ZFS_ERR_VDEV_TOO_BIG:
56586714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
56686714001SSerapheim Dimitropoulos 		break;
5677ac89354SDon Brady 	case ZFS_ERR_IOC_CMD_UNAVAIL:
5687ac89354SDon Brady 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
5697ac89354SDon Brady 		    "module does not support this operation. A reboot may "
5707ac89354SDon Brady 		    "be required to enable this operation."));
5717ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
5727ac89354SDon Brady 		break;
5737ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_UNAVAIL:
5747ac89354SDon Brady 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
5757ac89354SDon Brady 		    "module does not support an option for this operation. "
5767ac89354SDon Brady 		    "A reboot may be required to enable this option."));
5777ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
5787ac89354SDon Brady 		break;
5797ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_REQUIRED:
5807ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_BADTYPE:
5817ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
5827ac89354SDon Brady 		break;
58399653d4eSeschrock 	default:
58499653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
58599653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
58699653d4eSeschrock 	}
58799653d4eSeschrock 
58899653d4eSeschrock 	va_end(ap);
58999653d4eSeschrock 	return (-1);
590fa9e4066Sahrens }
591fa9e4066Sahrens 
592fa9e4066Sahrens /*
593fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
594fa9e4066Sahrens  */
59599653d4eSeschrock int
no_memory(libzfs_handle_t * hdl)59699653d4eSeschrock no_memory(libzfs_handle_t *hdl)
597fa9e4066Sahrens {
59899653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
599fa9e4066Sahrens }
600fa9e4066Sahrens 
601fa9e4066Sahrens /*
602fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
603fa9e4066Sahrens  */
604fa9e4066Sahrens void *
zfs_alloc(libzfs_handle_t * hdl,size_t size)60599653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
606fa9e4066Sahrens {
607fa9e4066Sahrens 	void *data;
608fa9e4066Sahrens 
609fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
61099653d4eSeschrock 		(void) no_memory(hdl);
611fa9e4066Sahrens 
612fa9e4066Sahrens 	return (data);
613fa9e4066Sahrens }
614fa9e4066Sahrens 
61599d5e173STim Haley /*
61699d5e173STim Haley  * A safe form of asprintf() which will die if the allocation fails.
61799d5e173STim Haley  */
61899d5e173STim Haley /*PRINTFLIKE2*/
61999d5e173STim Haley char *
zfs_asprintf(libzfs_handle_t * hdl,const char * fmt,...)62099d5e173STim Haley zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
62199d5e173STim Haley {
62299d5e173STim Haley 	va_list ap;
62399d5e173STim Haley 	char *ret;
62499d5e173STim Haley 	int err;
62599d5e173STim Haley 
62699d5e173STim Haley 	va_start(ap, fmt);
62799d5e173STim Haley 
62899d5e173STim Haley 	err = vasprintf(&ret, fmt, ap);
62999d5e173STim Haley 
63099d5e173STim Haley 	va_end(ap);
63199d5e173STim Haley 
63299d5e173STim Haley 	if (err < 0)
63399d5e173STim Haley 		(void) no_memory(hdl);
63499d5e173STim Haley 
63599d5e173STim Haley 	return (ret);
63699d5e173STim Haley }
63799d5e173STim Haley 
638e9dbad6fSeschrock /*
639e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
640e9dbad6fSeschrock  */
641e9dbad6fSeschrock void *
zfs_realloc(libzfs_handle_t * hdl,void * ptr,size_t oldsize,size_t newsize)642e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
643e9dbad6fSeschrock {
644e9dbad6fSeschrock 	void *ret;
645e9dbad6fSeschrock 
646e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
647e9dbad6fSeschrock 		(void) no_memory(hdl);
648e9dbad6fSeschrock 		return (NULL);
649e9dbad6fSeschrock 	}
650e9dbad6fSeschrock 
651e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
652e9dbad6fSeschrock 	return (ret);
653e9dbad6fSeschrock }
654e9dbad6fSeschrock 
655fa9e4066Sahrens /*
656fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
657fa9e4066Sahrens  */
658fa9e4066Sahrens char *
zfs_strdup(libzfs_handle_t * hdl,const char * str)65999653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
660fa9e4066Sahrens {
661fa9e4066Sahrens 	char *ret;
662fa9e4066Sahrens 
663fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
66499653d4eSeschrock 		(void) no_memory(hdl);
665fa9e4066Sahrens 
666fa9e4066Sahrens 	return (ret);
667fa9e4066Sahrens }
668fa9e4066Sahrens 
66999653d4eSeschrock void
libzfs_print_on_error(libzfs_handle_t * hdl,boolean_t printerr)67099653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
67199653d4eSeschrock {
67299653d4eSeschrock 	hdl->libzfs_printerr = printerr;
67399653d4eSeschrock }
67499653d4eSeschrock 
67599653d4eSeschrock libzfs_handle_t *
libzfs_init(void)67699653d4eSeschrock libzfs_init(void)
67799653d4eSeschrock {
67899653d4eSeschrock 	libzfs_handle_t *hdl;
679c5286370SAllan Jude 	int error;
680c5286370SAllan Jude 	char *env;
68199653d4eSeschrock 
68299d5e173STim Haley 	if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
68399653d4eSeschrock 		return (NULL);
68499653d4eSeschrock 	}
68599653d4eSeschrock 
6866c24238bSJason King 	if (regcomp(&hdl->libzfs_urire, URI_REGEX, REG_EXTENDED) != 0) {
6876c24238bSJason King 		free(hdl);
6886c24238bSJason King 		return (NULL);
6896c24238bSJason King 	}
6906c24238bSJason King 
691c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
6926c24238bSJason King 		regfree(&hdl->libzfs_urire);
69399653d4eSeschrock 		free(hdl);
69499653d4eSeschrock 		return (NULL);
69599653d4eSeschrock 	}
69699653d4eSeschrock 
697bde3d612SSimon Klinkert 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "rF")) == NULL) {
69899653d4eSeschrock 		(void) close(hdl->libzfs_fd);
6996c24238bSJason King 		regfree(&hdl->libzfs_urire);
70099653d4eSeschrock 		free(hdl);
70199653d4eSeschrock 		return (NULL);
70299653d4eSeschrock 	}
70399653d4eSeschrock 
704bde3d612SSimon Klinkert 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "rF");
70599653d4eSeschrock 
7064445fffbSMatthew Ahrens 	if (libzfs_core_init() != 0) {
7074445fffbSMatthew Ahrens 		(void) close(hdl->libzfs_fd);
7084445fffbSMatthew Ahrens 		(void) fclose(hdl->libzfs_mnttab);
7094445fffbSMatthew Ahrens 		(void) fclose(hdl->libzfs_sharetab);
7106c24238bSJason King 		regfree(&hdl->libzfs_urire);
7114445fffbSMatthew Ahrens 		free(hdl);
7124445fffbSMatthew Ahrens 		return (NULL);
7134445fffbSMatthew Ahrens 	}
7144445fffbSMatthew Ahrens 
71591ebeef5Sahrens 	zfs_prop_init();
716990b4856Slling 	zpool_prop_init();
717ad135b5dSChristopher Siden 	zpool_feature_init();
718b2634b9cSEric Taylor 	libzfs_mnttab_init(hdl);
719*0886dcadSAndy Fiddaman 	fletcher_4_init();
72091ebeef5Sahrens 
721dfc11533SChris Williamson 	if (getenv("ZFS_PROP_DEBUG") != NULL) {
722dfc11533SChris Williamson 		hdl->libzfs_prop_debug = B_TRUE;
723dfc11533SChris Williamson 	}
724c5286370SAllan Jude 	if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) {
725c5286370SAllan Jude 		if ((error = zfs_nicestrtonum(hdl, env,
726c5286370SAllan Jude 		    &hdl->libzfs_max_nvlist))) {
727c5286370SAllan Jude 			errno = error;
728c5286370SAllan Jude 			(void) close(hdl->libzfs_fd);
729c5286370SAllan Jude 			(void) fclose(hdl->libzfs_mnttab);
730c5286370SAllan Jude 			(void) fclose(hdl->libzfs_sharetab);
731c5286370SAllan Jude 			regfree(&hdl->libzfs_urire);
732c5286370SAllan Jude 			free(hdl);
733c5286370SAllan Jude 			return (NULL);
734c5286370SAllan Jude 		}
735c5286370SAllan Jude 	} else {
736c5286370SAllan Jude 		hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4);
737c5286370SAllan Jude 	}
738dfc11533SChris Williamson 
73999653d4eSeschrock 	return (hdl);
74099653d4eSeschrock }
74199653d4eSeschrock 
74299653d4eSeschrock void
libzfs_fini(libzfs_handle_t * hdl)74399653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
74499653d4eSeschrock {
74599653d4eSeschrock 	(void) close(hdl->libzfs_fd);
746727feae5SJoshua M. Clulow 	if (hdl->libzfs_mnttab != NULL)
74799653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
748727feae5SJoshua M. Clulow 	if (hdl->libzfs_sharetab != NULL)
74999653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
750727feae5SJoshua M. Clulow 	if (hdl->libzfs_devlink != NULL)
751727feae5SJoshua M. Clulow 		(void) di_devlink_fini(&hdl->libzfs_devlink);
75267331909Sdougm 	zfs_uninit_libshare(hdl);
75329ab75c9Srm 	zpool_free_handles(hdl);
754069f55e2SEric Schrock 	libzfs_fru_clear(hdl, B_TRUE);
75599653d4eSeschrock 	namespace_clear(hdl);
756b2634b9cSEric Taylor 	libzfs_mnttab_fini(hdl);
7574445fffbSMatthew Ahrens 	libzfs_core_fini();
7586c24238bSJason King 	regfree(&hdl->libzfs_urire);
759*0886dcadSAndy Fiddaman 	fletcher_4_fini();
76099653d4eSeschrock 	free(hdl);
76199653d4eSeschrock }
76299653d4eSeschrock 
76399653d4eSeschrock libzfs_handle_t *
zpool_get_handle(zpool_handle_t * zhp)76499653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
76599653d4eSeschrock {
76699653d4eSeschrock 	return (zhp->zpool_hdl);
76799653d4eSeschrock }
76899653d4eSeschrock 
76999653d4eSeschrock libzfs_handle_t *
zfs_get_handle(zfs_handle_t * zhp)77099653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
77199653d4eSeschrock {
77299653d4eSeschrock 	return (zhp->zfs_hdl);
77399653d4eSeschrock }
774e9dbad6fSeschrock 
775d5b5bb25SRich Morris zpool_handle_t *
zfs_get_pool_handle(const zfs_handle_t * zhp)776d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp)
777d5b5bb25SRich Morris {
778d5b5bb25SRich Morris 	return (zhp->zpool_hdl);
779d5b5bb25SRich Morris }
780d5b5bb25SRich Morris 
7815aba80dbSck /*
7825aba80dbSck  * Given a name, determine whether or not it's a valid path
7835aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
7845aba80dbSck  * to match the device number.  If not, treat the path as an
785edb901aaSMarcel Telka  * fs/vol/snap/bkmark name.
7865aba80dbSck  */
7875aba80dbSck zfs_handle_t *
zfs_path_to_zhandle(libzfs_handle_t * hdl,char * path,zfs_type_t argtype)7885aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
7895aba80dbSck {
7905aba80dbSck 	struct stat64 statbuf;
7915aba80dbSck 	struct extmnttab entry;
7925aba80dbSck 	int ret;
7935aba80dbSck 
7945aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
7955aba80dbSck 		/*
7965aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
7975aba80dbSck 		 */
7985aba80dbSck 		return (zfs_open(hdl, path, argtype));
7995aba80dbSck 	}
8005aba80dbSck 
8015aba80dbSck 	if (stat64(path, &statbuf) != 0) {
8025aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
8035aba80dbSck 		return (NULL);
8045aba80dbSck 	}
8055aba80dbSck 
8065aba80dbSck 	rewind(hdl->libzfs_mnttab);
8075aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
8085aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
8095aba80dbSck 		    statbuf.st_dev) {
8105aba80dbSck 			break;
8115aba80dbSck 		}
8125aba80dbSck 	}
8135aba80dbSck 	if (ret != 0) {
8145aba80dbSck 		return (NULL);
8155aba80dbSck 	}
8165aba80dbSck 
8175aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
8185aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
8195aba80dbSck 		    path);
8205aba80dbSck 		return (NULL);
8215aba80dbSck 	}
8225aba80dbSck 
8235aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
8245aba80dbSck }
8255aba80dbSck 
826e9dbad6fSeschrock /*
827e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
828e9dbad6fSeschrock  * an ioctl().
829e9dbad6fSeschrock  */
830e9dbad6fSeschrock int
zcmd_alloc_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,size_t len)831e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
832e9dbad6fSeschrock {
833e9dbad6fSeschrock 	if (len == 0)
8344b964adaSGeorge Wilson 		len = 16 * 1024;
835e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
83630925561SChris Williamson 	zc->zc_nvlist_dst =
83730925561SChris Williamson 	    (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
83830925561SChris Williamson 	if (zc->zc_nvlist_dst == 0)
839e9dbad6fSeschrock 		return (-1);
840e9dbad6fSeschrock 
841e9dbad6fSeschrock 	return (0);
842e9dbad6fSeschrock }
843e9dbad6fSeschrock 
844e9dbad6fSeschrock /*
845e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
846e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
847e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
848e9dbad6fSeschrock  */
849e9dbad6fSeschrock int
zcmd_expand_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc)850e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
851e9dbad6fSeschrock {
852e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
85330925561SChris Williamson 	zc->zc_nvlist_dst =
85430925561SChris Williamson 	    (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
85530925561SChris Williamson 	if (zc->zc_nvlist_dst == 0)
856e9dbad6fSeschrock 		return (-1);
857e9dbad6fSeschrock 
858e9dbad6fSeschrock 	return (0);
859e9dbad6fSeschrock }
860e9dbad6fSeschrock 
861e9dbad6fSeschrock /*
862a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
863e9dbad6fSeschrock  */
864e9dbad6fSeschrock void
zcmd_free_nvlists(zfs_cmd_t * zc)865e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
866e9dbad6fSeschrock {
867990b4856Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
868e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
869e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
8705b8cbb8eSToomas Soome 	zc->zc_nvlist_conf = 0;
8715b8cbb8eSToomas Soome 	zc->zc_nvlist_src = 0;
8725b8cbb8eSToomas Soome 	zc->zc_nvlist_dst = 0;
873e9dbad6fSeschrock }
874e9dbad6fSeschrock 
875990b4856Slling static int
zcmd_write_nvlist_com(libzfs_handle_t * hdl,uint64_t * outnv,uint64_t * outlen,nvlist_t * nvl)876990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
877990b4856Slling     nvlist_t *nvl)
878e9dbad6fSeschrock {
879e9dbad6fSeschrock 	char *packed;
880e9dbad6fSeschrock 	size_t len;
881e9dbad6fSeschrock 
882e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
883e9dbad6fSeschrock 
884e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
885e9dbad6fSeschrock 		return (-1);
886e9dbad6fSeschrock 
887e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
888e9dbad6fSeschrock 
889990b4856Slling 	*outnv = (uint64_t)(uintptr_t)packed;
890990b4856Slling 	*outlen = len;
891e9dbad6fSeschrock 
892e9dbad6fSeschrock 	return (0);
893e9dbad6fSeschrock }
894e9dbad6fSeschrock 
895990b4856Slling int
zcmd_write_conf_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t * nvl)896990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
897990b4856Slling {
898990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
899990b4856Slling 	    &zc->zc_nvlist_conf_size, nvl));
900990b4856Slling }
901990b4856Slling 
902990b4856Slling int
zcmd_write_src_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t * nvl)903990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
904990b4856Slling {
905990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
906990b4856Slling 	    &zc->zc_nvlist_src_size, nvl));
907990b4856Slling }
908990b4856Slling 
909e9dbad6fSeschrock /*
910e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
911e9dbad6fSeschrock  */
912e9dbad6fSeschrock int
zcmd_read_dst_nvlist(libzfs_handle_t * hdl,zfs_cmd_t * zc,nvlist_t ** nvlp)913e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
914e9dbad6fSeschrock {
915e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
916e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
917e9dbad6fSeschrock 		return (no_memory(hdl));
918e9dbad6fSeschrock 
919e9dbad6fSeschrock 	return (0);
920e9dbad6fSeschrock }
921b1b8ab34Slling 
922990b4856Slling int
zfs_ioctl(libzfs_handle_t * hdl,int request,zfs_cmd_t * zc)923990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
924990b4856Slling {
9254445fffbSMatthew Ahrens 	return (ioctl(hdl->libzfs_fd, request, zc));
926990b4856Slling }
927990b4856Slling 
928990b4856Slling /*
929990b4856Slling  * ================================================================
930990b4856Slling  * API shared by zfs and zpool property management
931990b4856Slling  * ================================================================
932990b4856Slling  */
933990b4856Slling 
934b1b8ab34Slling static void
zprop_print_headers(zprop_get_cbdata_t * cbp,zfs_type_t type)935990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
936b1b8ab34Slling {
937990b4856Slling 	zprop_list_t *pl = cbp->cb_proplist;
938b1b8ab34Slling 	int i;
939b1b8ab34Slling 	char *title;
940b1b8ab34Slling 	size_t len;
941b1b8ab34Slling 
942b1b8ab34Slling 	cbp->cb_first = B_FALSE;
943b1b8ab34Slling 	if (cbp->cb_scripted)
944b1b8ab34Slling 		return;
945b1b8ab34Slling 
946b1b8ab34Slling 	/*
947b1b8ab34Slling 	 * Start with the length of the column headers.
948b1b8ab34Slling 	 */
949b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
950b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
951b1b8ab34Slling 	    "PROPERTY"));
952b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
953b1b8ab34Slling 	    "VALUE"));
95492241e0bSTom Erickson 	cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
95592241e0bSTom Erickson 	    "RECEIVED"));
956b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
957b1b8ab34Slling 	    "SOURCE"));
958b1b8ab34Slling 
959deb8317bSMark J Musante 	/* first property is always NAME */
960deb8317bSMark J Musante 	assert(cbp->cb_proplist->pl_prop ==
961deb8317bSMark J Musante 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
962deb8317bSMark J Musante 
963b1b8ab34Slling 	/*
964b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
965b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
966b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
967b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
968b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
96992241e0bSTom Erickson 	 * if the name of the property is much longer than any values we find.
970b1b8ab34Slling 	 */
971b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
972b1b8ab34Slling 		/*
973b1b8ab34Slling 		 * 'PROPERTY' column
974b1b8ab34Slling 		 */
975990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
976990b4856Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
977990b4856Slling 			    zpool_prop_to_name(pl->pl_prop) :
978990b4856Slling 			    zfs_prop_to_name(pl->pl_prop);
979990b4856Slling 
980990b4856Slling 			len = strlen(propname);
981b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
982b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
983b1b8ab34Slling 		} else {
984b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
985b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
986b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
987b1b8ab34Slling 		}
988b1b8ab34Slling 
989b1b8ab34Slling 		/*
990deb8317bSMark J Musante 		 * 'VALUE' column.  The first property is always the 'name'
991deb8317bSMark J Musante 		 * property that was tacked on either by /sbin/zfs's
992deb8317bSMark J Musante 		 * zfs_do_get() or when calling zprop_expand_list(), so we
993deb8317bSMark J Musante 		 * ignore its width.  If the user specified the name property
994deb8317bSMark J Musante 		 * to display, then it will be later in the list in any case.
995b1b8ab34Slling 		 */
996deb8317bSMark J Musante 		if (pl != cbp->cb_proplist &&
997b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
998b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
999b1b8ab34Slling 
100092241e0bSTom Erickson 		/* 'RECEIVED' column. */
100192241e0bSTom Erickson 		if (pl != cbp->cb_proplist &&
100292241e0bSTom Erickson 		    pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
100392241e0bSTom Erickson 			cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
100492241e0bSTom Erickson 
1005b1b8ab34Slling 		/*
1006b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
1007b1b8ab34Slling 		 */
1008990b4856Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
1009990b4856Slling 		    ZFS_PROP_NAME) &&
1010b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
1011b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
1012b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
1013b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
1014b1b8ab34Slling 		}
1015b1b8ab34Slling 	}
1016b1b8ab34Slling 
1017b1b8ab34Slling 	/*
1018b1b8ab34Slling 	 * Now go through and print the headers.
1019b1b8ab34Slling 	 */
102092241e0bSTom Erickson 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
1021b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
1022b1b8ab34Slling 		case GET_COL_NAME:
1023b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
1024b1b8ab34Slling 			break;
1025b1b8ab34Slling 		case GET_COL_PROPERTY:
1026b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
1027b1b8ab34Slling 			break;
1028b1b8ab34Slling 		case GET_COL_VALUE:
1029b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
1030b1b8ab34Slling 			break;
103192241e0bSTom Erickson 		case GET_COL_RECVD:
103292241e0bSTom Erickson 			title = dgettext(TEXT_DOMAIN, "RECEIVED");
103392241e0bSTom Erickson 			break;
1034b1b8ab34Slling 		case GET_COL_SOURCE:
1035b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
1036b1b8ab34Slling 			break;
1037b1b8ab34Slling 		default:
1038b1b8ab34Slling 			title = NULL;
1039b1b8ab34Slling 		}
1040b1b8ab34Slling 
1041b1b8ab34Slling 		if (title != NULL) {
104292241e0bSTom Erickson 			if (i == (ZFS_GET_NCOLS - 1) ||
104392241e0bSTom Erickson 			    cbp->cb_columns[i + 1] == GET_COL_NONE)
1044b1b8ab34Slling 				(void) printf("%s", title);
1045b1b8ab34Slling 			else
1046b1b8ab34Slling 				(void) printf("%-*s  ",
1047b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
1048b1b8ab34Slling 				    title);
1049b1b8ab34Slling 		}
1050b1b8ab34Slling 	}
1051b1b8ab34Slling 	(void) printf("\n");
1052b1b8ab34Slling }
1053b1b8ab34Slling 
1054b1b8ab34Slling /*
1055b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
1056b1b8ab34Slling  * structure.
1057b1b8ab34Slling  */
1058b1b8ab34Slling void
zprop_print_one_property(const char * name,zprop_get_cbdata_t * cbp,const char * propname,const char * value,zprop_source_t sourcetype,const char * source,const char * recvd_value)1059990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
1060990b4856Slling     const char *propname, const char *value, zprop_source_t sourcetype,
106192241e0bSTom Erickson     const char *source, const char *recvd_value)
1062b1b8ab34Slling {
1063b1b8ab34Slling 	int i;
1064f83b46baSPaul Dagnelie 	const char *str = NULL;
1065b1b8ab34Slling 	char buf[128];
1066b1b8ab34Slling 
1067b1b8ab34Slling 	/*
1068b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
1069b1b8ab34Slling 	 */
1070b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
1071b1b8ab34Slling 		return;
1072b1b8ab34Slling 
1073b1b8ab34Slling 	if (cbp->cb_first)
1074990b4856Slling 		zprop_print_headers(cbp, cbp->cb_type);
1075b1b8ab34Slling 
107692241e0bSTom Erickson 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
1077b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
1078b1b8ab34Slling 		case GET_COL_NAME:
1079b1b8ab34Slling 			str = name;
1080b1b8ab34Slling 			break;
1081b1b8ab34Slling 
1082b1b8ab34Slling 		case GET_COL_PROPERTY:
1083b1b8ab34Slling 			str = propname;
1084b1b8ab34Slling 			break;
1085b1b8ab34Slling 
1086b1b8ab34Slling 		case GET_COL_VALUE:
1087b1b8ab34Slling 			str = value;
1088b1b8ab34Slling 			break;
1089b1b8ab34Slling 
1090b1b8ab34Slling 		case GET_COL_SOURCE:
1091b1b8ab34Slling 			switch (sourcetype) {
1092990b4856Slling 			case ZPROP_SRC_NONE:
1093b1b8ab34Slling 				str = "-";
1094b1b8ab34Slling 				break;
1095b1b8ab34Slling 
1096990b4856Slling 			case ZPROP_SRC_DEFAULT:
1097b1b8ab34Slling 				str = "default";
1098b1b8ab34Slling 				break;
1099b1b8ab34Slling 
1100990b4856Slling 			case ZPROP_SRC_LOCAL:
1101b1b8ab34Slling 				str = "local";
1102b1b8ab34Slling 				break;
1103b1b8ab34Slling 
1104990b4856Slling 			case ZPROP_SRC_TEMPORARY:
1105b1b8ab34Slling 				str = "temporary";
1106b1b8ab34Slling 				break;
1107b1b8ab34Slling 
1108990b4856Slling 			case ZPROP_SRC_INHERITED:
1109b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
1110b1b8ab34Slling 				    "inherited from %s", source);
1111b1b8ab34Slling 				str = buf;
1112b1b8ab34Slling 				break;
111392241e0bSTom Erickson 			case ZPROP_SRC_RECEIVED:
111492241e0bSTom Erickson 				str = "received";
111592241e0bSTom Erickson 				break;
111688f61deeSIgor Kozhukhov 
111788f61deeSIgor Kozhukhov 			default:
111888f61deeSIgor Kozhukhov 				str = NULL;
111988f61deeSIgor Kozhukhov 				assert(!"unhandled zprop_source_t");
1120b1b8ab34Slling 			}
1121b1b8ab34Slling 			break;
1122b1b8ab34Slling 
112392241e0bSTom Erickson 		case GET_COL_RECVD:
112492241e0bSTom Erickson 			str = (recvd_value == NULL ? "-" : recvd_value);
112592241e0bSTom Erickson 			break;
112692241e0bSTom Erickson 
1127b1b8ab34Slling 		default:
1128b1b8ab34Slling 			continue;
1129b1b8ab34Slling 		}
1130b1b8ab34Slling 
113192241e0bSTom Erickson 		if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1132b1b8ab34Slling 			(void) printf("%s", str);
1133b1b8ab34Slling 		else if (cbp->cb_scripted)
1134b1b8ab34Slling 			(void) printf("%s\t", str);
1135b1b8ab34Slling 		else
1136b1b8ab34Slling 			(void) printf("%-*s  ",
1137b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
1138b1b8ab34Slling 			    str);
1139b1b8ab34Slling 	}
1140b1b8ab34Slling 
1141b1b8ab34Slling 	(void) printf("\n");
1142b1b8ab34Slling }
1143ecd6cf80Smarks 
1144990b4856Slling /*
1145990b4856Slling  * Given a numeric suffix, convert the value into a number of bits that the
1146990b4856Slling  * resulting value must be shifted.
1147990b4856Slling  */
1148990b4856Slling static int
str2shift(libzfs_handle_t * hdl,const char * buf)1149990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf)
1150990b4856Slling {
1151990b4856Slling 	const char *ends = "BKMGTPEZ";
1152990b4856Slling 	int i;
1153990b4856Slling 
1154990b4856Slling 	if (buf[0] == '\0')
1155990b4856Slling 		return (0);
1156990b4856Slling 	for (i = 0; i < strlen(ends); i++) {
1157990b4856Slling 		if (toupper(buf[0]) == ends[i])
1158990b4856Slling 			break;
1159990b4856Slling 	}
1160990b4856Slling 	if (i == strlen(ends)) {
11619e494b8aSToomas Soome 		if (hdl != NULL)
11629e494b8aSToomas Soome 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11639e494b8aSToomas Soome 			    "invalid numeric suffix '%s'"), buf);
1164990b4856Slling 		return (-1);
1165990b4856Slling 	}
1166990b4856Slling 
1167990b4856Slling 	/*
1168990b4856Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
1169990b4856Slling 	 * allow 'BB' - that's just weird.
1170990b4856Slling 	 */
1171990b4856Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1172990b4856Slling 	    toupper(buf[0]) != 'B'))
11739e494b8aSToomas Soome 		return (10 * i);
1174990b4856Slling 
11759e494b8aSToomas Soome 	if (hdl != NULL)
11769e494b8aSToomas Soome 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11779e494b8aSToomas Soome 		    "invalid numeric suffix '%s'"), buf);
1178990b4856Slling 	return (-1);
1179990b4856Slling }
1180990b4856Slling 
1181990b4856Slling /*
1182990b4856Slling  * Convert a string of the form '100G' into a real number.  Used when setting
1183990b4856Slling  * properties or creating a volume.  'buf' is used to place an extended error
1184990b4856Slling  * message for the caller to use.
1185990b4856Slling  */
1186ecd6cf80Smarks int
zfs_nicestrtonum(libzfs_handle_t * hdl,const char * value,uint64_t * num)1187990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1188ecd6cf80Smarks {
1189990b4856Slling 	char *end;
1190990b4856Slling 	int shift;
1191ecd6cf80Smarks 
1192990b4856Slling 	*num = 0;
1193990b4856Slling 
1194990b4856Slling 	/* Check to see if this looks like a number.  */
1195990b4856Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1196990b4856Slling 		if (hdl)
1197990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198990b4856Slling 			    "bad numeric value '%s'"), value);
1199990b4856Slling 		return (-1);
1200ecd6cf80Smarks 	}
1201ecd6cf80Smarks 
1202069f55e2SEric Schrock 	/* Rely on strtoull() to process the numeric portion.  */
1203990b4856Slling 	errno = 0;
1204f67f35c3SEric Schrock 	*num = strtoull(value, &end, 10);
1205990b4856Slling 
1206990b4856Slling 	/*
1207990b4856Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
1208990b4856Slling 	 * in a 64-bit value.
1209990b4856Slling 	 */
1210990b4856Slling 	if (errno == ERANGE) {
1211990b4856Slling 		if (hdl)
1212990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1213990b4856Slling 			    "numeric value is too large"));
1214990b4856Slling 		return (-1);
1215990b4856Slling 	}
1216990b4856Slling 
1217990b4856Slling 	/*
1218990b4856Slling 	 * If we have a decimal value, then do the computation with floating
1219990b4856Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1220990b4856Slling 	 */
1221990b4856Slling 	if (*end == '.') {
1222990b4856Slling 		double fval = strtod(value, &end);
1223990b4856Slling 
1224990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1225990b4856Slling 			return (-1);
1226990b4856Slling 
1227990b4856Slling 		fval *= pow(2, shift);
1228990b4856Slling 
1229990b4856Slling 		if (fval > UINT64_MAX) {
1230990b4856Slling 			if (hdl)
1231990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1232990b4856Slling 				    "numeric value is too large"));
1233990b4856Slling 			return (-1);
1234990b4856Slling 		}
1235990b4856Slling 
1236990b4856Slling 		*num = (uint64_t)fval;
1237990b4856Slling 	} else {
1238990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1239990b4856Slling 			return (-1);
1240990b4856Slling 
1241990b4856Slling 		/* Check for overflow */
1242990b4856Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1243990b4856Slling 			if (hdl)
1244990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1245990b4856Slling 				    "numeric value is too large"));
1246990b4856Slling 			return (-1);
1247990b4856Slling 		}
1248990b4856Slling 
1249990b4856Slling 		*num <<= shift;
1250990b4856Slling 	}
1251990b4856Slling 
1252990b4856Slling 	return (0);
1253990b4856Slling }
1254990b4856Slling 
1255990b4856Slling /*
1256990b4856Slling  * Given a propname=value nvpair to set, parse any numeric properties
1257990b4856Slling  * (index, boolean, etc) if they are specified as strings and add the
1258990b4856Slling  * resulting nvpair to the returned nvlist.
1259990b4856Slling  *
1260990b4856Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1261990b4856Slling  * We want the user to be able to ignore this fact and specify properties
1262990b4856Slling  * as native values (numbers, for example) or as strings (to simplify
1263990b4856Slling  * command line utilities).  This also handles converting index types
1264990b4856Slling  * (compression, checksum, etc) from strings to their on-disk index.
1265990b4856Slling  */
1266990b4856Slling int
zprop_parse_value(libzfs_handle_t * hdl,nvpair_t * elem,int prop,zfs_type_t type,nvlist_t * ret,char ** svalp,uint64_t * ivalp,const char * errbuf)1267990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1268990b4856Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1269990b4856Slling     const char *errbuf)
1270990b4856Slling {
1271990b4856Slling 	data_type_t datatype = nvpair_type(elem);
1272990b4856Slling 	zprop_type_t proptype;
1273990b4856Slling 	const char *propname;
1274990b4856Slling 	char *value;
1275990b4856Slling 	boolean_t isnone = B_FALSE;
12761c10ae76SMike Gerdts 	boolean_t isauto = B_FALSE;
1277990b4856Slling 
1278990b4856Slling 	if (type == ZFS_TYPE_POOL) {
1279990b4856Slling 		proptype = zpool_prop_get_type(prop);
1280990b4856Slling 		propname = zpool_prop_to_name(prop);
1281990b4856Slling 	} else {
1282990b4856Slling 		proptype = zfs_prop_get_type(prop);
1283990b4856Slling 		propname = zfs_prop_to_name(prop);
1284990b4856Slling 	}
1285990b4856Slling 
1286990b4856Slling 	/*
1287990b4856Slling 	 * Convert any properties to the internal DSL value types.
1288990b4856Slling 	 */
1289990b4856Slling 	*svalp = NULL;
1290990b4856Slling 	*ivalp = 0;
1291990b4856Slling 
1292990b4856Slling 	switch (proptype) {
1293990b4856Slling 	case PROP_TYPE_STRING:
1294990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1295990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1296990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1297990b4856Slling 			goto error;
1298990b4856Slling 		}
1299990b4856Slling 		(void) nvpair_value_string(elem, svalp);
1300990b4856Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1301990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1302990b4856Slling 			    "'%s' is too long"), nvpair_name(elem));
1303990b4856Slling 			goto error;
1304990b4856Slling 		}
1305990b4856Slling 		break;
1306990b4856Slling 
1307990b4856Slling 	case PROP_TYPE_NUMBER:
1308990b4856Slling 		if (datatype == DATA_TYPE_STRING) {
1309990b4856Slling 			(void) nvpair_value_string(elem, &value);
1310990b4856Slling 			if (strcmp(value, "none") == 0) {
1311990b4856Slling 				isnone = B_TRUE;
13121c10ae76SMike Gerdts 			} else if (strcmp(value, "auto") == 0) {
13131c10ae76SMike Gerdts 				isauto = B_TRUE;
13141c10ae76SMike Gerdts 			} else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1315990b4856Slling 				goto error;
1316990b4856Slling 			}
1317990b4856Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1318990b4856Slling 			(void) nvpair_value_uint64(elem, ivalp);
1319990b4856Slling 		} else {
1320990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1321990b4856Slling 			    "'%s' must be a number"), nvpair_name(elem));
1322990b4856Slling 			goto error;
1323990b4856Slling 		}
1324990b4856Slling 
1325990b4856Slling 		/*
1326990b4856Slling 		 * Quota special: force 'none' and don't allow 0.
1327990b4856Slling 		 */
1328a9799022Sck 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1329a9799022Sck 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1330990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1331a9799022Sck 			    "use 'none' to disable quota/refquota"));
1332990b4856Slling 			goto error;
1333990b4856Slling 		}
1334a2afb611SJerry Jelinek 
1335a2afb611SJerry Jelinek 		/*
1336a2afb611SJerry Jelinek 		 * Special handling for "*_limit=none". In this case it's not
1337a2afb611SJerry Jelinek 		 * 0 but UINT64_MAX.
1338a2afb611SJerry Jelinek 		 */
1339a2afb611SJerry Jelinek 		if ((type & ZFS_TYPE_DATASET) && isnone &&
1340a2afb611SJerry Jelinek 		    (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1341a2afb611SJerry Jelinek 		    prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1342a2afb611SJerry Jelinek 			*ivalp = UINT64_MAX;
1343a2afb611SJerry Jelinek 		}
13441c10ae76SMike Gerdts 
13451c10ae76SMike Gerdts 		/*
13461c10ae76SMike Gerdts 		 * Special handling for setting 'refreservation' to 'auto'.  Use
13471c10ae76SMike Gerdts 		 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
13481c10ae76SMike Gerdts 		 * 'auto' is only allowed on volumes.
13491c10ae76SMike Gerdts 		 */
13501c10ae76SMike Gerdts 		if (isauto) {
13511c10ae76SMike Gerdts 			switch (prop) {
13521c10ae76SMike Gerdts 			case ZFS_PROP_REFRESERVATION:
13531c10ae76SMike Gerdts 				if ((type & ZFS_TYPE_VOLUME) == 0) {
13541c10ae76SMike Gerdts 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13551c10ae76SMike Gerdts 					    "'%s=auto' only allowed on "
13561c10ae76SMike Gerdts 					    "volumes"), nvpair_name(elem));
13571c10ae76SMike Gerdts 					goto error;
13581c10ae76SMike Gerdts 				}
13591c10ae76SMike Gerdts 				*ivalp = UINT64_MAX;
13601c10ae76SMike Gerdts 				break;
13611c10ae76SMike Gerdts 			default:
13621c10ae76SMike Gerdts 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
13631c10ae76SMike Gerdts 				    "'auto' is invalid value for '%s'"),
13641c10ae76SMike Gerdts 				    nvpair_name(elem));
13651c10ae76SMike Gerdts 				goto error;
13661c10ae76SMike Gerdts 			}
13671c10ae76SMike Gerdts 		}
13681c10ae76SMike Gerdts 
1369990b4856Slling 		break;
1370990b4856Slling 
1371990b4856Slling 	case PROP_TYPE_INDEX:
1372a9799022Sck 		if (datatype != DATA_TYPE_STRING) {
1373990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1374990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1375990b4856Slling 			goto error;
1376990b4856Slling 		}
1377990b4856Slling 
1378a9799022Sck 		(void) nvpair_value_string(elem, &value);
1379a9799022Sck 
1380990b4856Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1381990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1382990b4856Slling 			    "'%s' must be one of '%s'"), propname,
1383990b4856Slling 			    zprop_values(prop, type));
1384990b4856Slling 			goto error;
1385990b4856Slling 		}
1386990b4856Slling 		break;
1387990b4856Slling 
1388990b4856Slling 	default:
1389990b4856Slling 		abort();
1390990b4856Slling 	}
1391990b4856Slling 
1392990b4856Slling 	/*
1393990b4856Slling 	 * Add the result to our return set of properties.
1394990b4856Slling 	 */
1395990b4856Slling 	if (*svalp != NULL) {
1396990b4856Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1397990b4856Slling 			(void) no_memory(hdl);
1398990b4856Slling 			return (-1);
1399990b4856Slling 		}
1400990b4856Slling 	} else {
1401990b4856Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1402990b4856Slling 			(void) no_memory(hdl);
1403990b4856Slling 			return (-1);
1404990b4856Slling 		}
1405990b4856Slling 	}
1406990b4856Slling 
1407990b4856Slling 	return (0);
1408990b4856Slling error:
1409990b4856Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1410990b4856Slling 	return (-1);
1411990b4856Slling }
1412990b4856Slling 
141374e7dc98SMatthew Ahrens static int
addlist(libzfs_handle_t * hdl,char * propname,zprop_list_t ** listp,zfs_type_t type)141474e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
141574e7dc98SMatthew Ahrens     zfs_type_t type)
141674e7dc98SMatthew Ahrens {
141774e7dc98SMatthew Ahrens 	int prop;
141874e7dc98SMatthew Ahrens 	zprop_list_t *entry;
141974e7dc98SMatthew Ahrens 
142074e7dc98SMatthew Ahrens 	prop = zprop_name_to_prop(propname, type);
142174e7dc98SMatthew Ahrens 
14224c2bdae2STim Chase 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE))
142374e7dc98SMatthew Ahrens 		prop = ZPROP_INVAL;
142474e7dc98SMatthew Ahrens 
142574e7dc98SMatthew Ahrens 	/*
142674e7dc98SMatthew Ahrens 	 * When no property table entry can be found, return failure if
142774e7dc98SMatthew Ahrens 	 * this is a pool property or if this isn't a user-defined
142874e7dc98SMatthew Ahrens 	 * dataset property,
142974e7dc98SMatthew Ahrens 	 */
1430ad135b5dSChristopher Siden 	if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1431ad135b5dSChristopher Siden 	    !zpool_prop_feature(propname) &&
1432ad135b5dSChristopher Siden 	    !zpool_prop_unsupported(propname)) ||
1433ad135b5dSChristopher Siden 	    (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1434ad135b5dSChristopher Siden 	    !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
143574e7dc98SMatthew Ahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143674e7dc98SMatthew Ahrens 		    "invalid property '%s'"), propname);
143774e7dc98SMatthew Ahrens 		return (zfs_error(hdl, EZFS_BADPROP,
143874e7dc98SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "bad property list")));
143974e7dc98SMatthew Ahrens 	}
144074e7dc98SMatthew Ahrens 
144174e7dc98SMatthew Ahrens 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
144274e7dc98SMatthew Ahrens 		return (-1);
144374e7dc98SMatthew Ahrens 
144474e7dc98SMatthew Ahrens 	entry->pl_prop = prop;
144574e7dc98SMatthew Ahrens 	if (prop == ZPROP_INVAL) {
1446ad135b5dSChristopher Siden 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1447ad135b5dSChristopher Siden 		    NULL) {
144874e7dc98SMatthew Ahrens 			free(entry);
144974e7dc98SMatthew Ahrens 			return (-1);
145074e7dc98SMatthew Ahrens 		}
145174e7dc98SMatthew Ahrens 		entry->pl_width = strlen(propname);
145274e7dc98SMatthew Ahrens 	} else {
145374e7dc98SMatthew Ahrens 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
145474e7dc98SMatthew Ahrens 		    type);
145574e7dc98SMatthew Ahrens 	}
145674e7dc98SMatthew Ahrens 
145774e7dc98SMatthew Ahrens 	*listp = entry;
145874e7dc98SMatthew Ahrens 
145974e7dc98SMatthew Ahrens 	return (0);
146074e7dc98SMatthew Ahrens }
146174e7dc98SMatthew Ahrens 
1462990b4856Slling /*
1463990b4856Slling  * Given a comma-separated list of properties, construct a property list
1464990b4856Slling  * containing both user-defined and native properties.  This function will
1465990b4856Slling  * return a NULL list if 'all' is specified, which can later be expanded
1466990b4856Slling  * by zprop_expand_list().
1467990b4856Slling  */
1468990b4856Slling int
zprop_get_list(libzfs_handle_t * hdl,char * props,zprop_list_t ** listp,zfs_type_t type)1469990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1470990b4856Slling     zfs_type_t type)
1471990b4856Slling {
1472990b4856Slling 	*listp = NULL;
1473990b4856Slling 
1474990b4856Slling 	/*
1475990b4856Slling 	 * If 'all' is specified, return a NULL list.
1476990b4856Slling 	 */
1477990b4856Slling 	if (strcmp(props, "all") == 0)
1478990b4856Slling 		return (0);
1479990b4856Slling 
1480990b4856Slling 	/*
1481990b4856Slling 	 * If no props were specified, return an error.
1482990b4856Slling 	 */
1483990b4856Slling 	if (props[0] == '\0') {
1484990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1485990b4856Slling 		    "no properties specified"));
1486990b4856Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1487990b4856Slling 		    "bad property list")));
1488990b4856Slling 	}
1489990b4856Slling 
1490990b4856Slling 	/*
1491990b4856Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1492990b4856Slling 	 * aliases makes this more effort than it's worth.
1493990b4856Slling 	 */
149474e7dc98SMatthew Ahrens 	while (*props != '\0') {
149574e7dc98SMatthew Ahrens 		size_t len;
149674e7dc98SMatthew Ahrens 		char *p;
149774e7dc98SMatthew Ahrens 		char c;
149874e7dc98SMatthew Ahrens 
149974e7dc98SMatthew Ahrens 		if ((p = strchr(props, ',')) == NULL) {
150074e7dc98SMatthew Ahrens 			len = strlen(props);
150174e7dc98SMatthew Ahrens 			p = props + len;
1502990b4856Slling 		} else {
150374e7dc98SMatthew Ahrens 			len = p - props;
1504990b4856Slling 		}
1505990b4856Slling 
1506990b4856Slling 		/*
1507990b4856Slling 		 * Check for empty options.
1508990b4856Slling 		 */
1509990b4856Slling 		if (len == 0) {
1510990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511990b4856Slling 			    "empty property name"));
1512990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1513990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1514990b4856Slling 		}
1515990b4856Slling 
1516990b4856Slling 		/*
1517990b4856Slling 		 * Check all regular property names.
1518990b4856Slling 		 */
151974e7dc98SMatthew Ahrens 		c = props[len];
152074e7dc98SMatthew Ahrens 		props[len] = '\0';
152174e7dc98SMatthew Ahrens 
152274e7dc98SMatthew Ahrens 		if (strcmp(props, "space") == 0) {
152374e7dc98SMatthew Ahrens 			static char *spaceprops[] = {
152474e7dc98SMatthew Ahrens 				"name", "avail", "used", "usedbysnapshots",
152574e7dc98SMatthew Ahrens 				"usedbydataset", "usedbyrefreservation",
152674e7dc98SMatthew Ahrens 				"usedbychildren", NULL
152774e7dc98SMatthew Ahrens 			};
152874e7dc98SMatthew Ahrens 			int i;
152974e7dc98SMatthew Ahrens 
153074e7dc98SMatthew Ahrens 			for (i = 0; spaceprops[i]; i++) {
153174e7dc98SMatthew Ahrens 				if (addlist(hdl, spaceprops[i], listp, type))
153274e7dc98SMatthew Ahrens 					return (-1);
153374e7dc98SMatthew Ahrens 				listp = &(*listp)->pl_next;
1534990b4856Slling 			}
1535990b4856Slling 		} else {
153674e7dc98SMatthew Ahrens 			if (addlist(hdl, props, listp, type))
153774e7dc98SMatthew Ahrens 				return (-1);
153874e7dc98SMatthew Ahrens 			listp = &(*listp)->pl_next;
1539990b4856Slling 		}
1540990b4856Slling 
154174e7dc98SMatthew Ahrens 		props = p;
1542990b4856Slling 		if (c == ',')
154374e7dc98SMatthew Ahrens 			props++;
1544990b4856Slling 	}
1545990b4856Slling 
1546990b4856Slling 	return (0);
1547990b4856Slling }
1548990b4856Slling 
1549990b4856Slling void
zprop_free_list(zprop_list_t * pl)1550990b4856Slling zprop_free_list(zprop_list_t *pl)
1551990b4856Slling {
1552990b4856Slling 	zprop_list_t *next;
1553990b4856Slling 
1554990b4856Slling 	while (pl != NULL) {
1555990b4856Slling 		next = pl->pl_next;
1556990b4856Slling 		free(pl->pl_user_prop);
1557990b4856Slling 		free(pl);
1558990b4856Slling 		pl = next;
1559990b4856Slling 	}
1560990b4856Slling }
1561990b4856Slling 
1562990b4856Slling typedef struct expand_data {
1563990b4856Slling 	zprop_list_t	**last;
1564990b4856Slling 	libzfs_handle_t	*hdl;
1565990b4856Slling 	zfs_type_t type;
1566990b4856Slling } expand_data_t;
1567990b4856Slling 
1568990b4856Slling int
zprop_expand_list_cb(int prop,void * cb)1569990b4856Slling zprop_expand_list_cb(int prop, void *cb)
1570990b4856Slling {
1571990b4856Slling 	zprop_list_t *entry;
1572990b4856Slling 	expand_data_t *edp = cb;
1573990b4856Slling 
1574990b4856Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1575990b4856Slling 		return (ZPROP_INVAL);
1576990b4856Slling 
1577990b4856Slling 	entry->pl_prop = prop;
1578990b4856Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1579990b4856Slling 	entry->pl_all = B_TRUE;
1580990b4856Slling 
1581990b4856Slling 	*(edp->last) = entry;
1582990b4856Slling 	edp->last = &entry->pl_next;
1583990b4856Slling 
1584990b4856Slling 	return (ZPROP_CONT);
1585990b4856Slling }
1586990b4856Slling 
1587990b4856Slling int
zprop_expand_list(libzfs_handle_t * hdl,zprop_list_t ** plp,zfs_type_t type)1588990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1589990b4856Slling {
1590990b4856Slling 	zprop_list_t *entry;
1591990b4856Slling 	zprop_list_t **last;
1592990b4856Slling 	expand_data_t exp;
1593990b4856Slling 
1594990b4856Slling 	if (*plp == NULL) {
1595990b4856Slling 		/*
1596990b4856Slling 		 * If this is the very first time we've been called for an 'all'
1597990b4856Slling 		 * specification, expand the list to include all native
1598990b4856Slling 		 * properties.
1599990b4856Slling 		 */
1600990b4856Slling 		last = plp;
1601990b4856Slling 
1602990b4856Slling 		exp.last = last;
1603990b4856Slling 		exp.hdl = hdl;
1604990b4856Slling 		exp.type = type;
1605990b4856Slling 
1606990b4856Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1607990b4856Slling 		    B_FALSE, type) == ZPROP_INVAL)
1608990b4856Slling 			return (-1);
1609990b4856Slling 
1610990b4856Slling 		/*
1611990b4856Slling 		 * Add 'name' to the beginning of the list, which is handled
1612990b4856Slling 		 * specially.
1613990b4856Slling 		 */
1614990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1615990b4856Slling 			return (-1);
1616990b4856Slling 
1617990b4856Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1618990b4856Slling 		    ZFS_PROP_NAME;
1619990b4856Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1620990b4856Slling 		    &entry->pl_fixed, type);
1621990b4856Slling 		entry->pl_all = B_TRUE;
1622990b4856Slling 		entry->pl_next = *plp;
1623990b4856Slling 		*plp = entry;
1624990b4856Slling 	}
1625990b4856Slling 	return (0);
1626990b4856Slling }
1627990b4856Slling 
1628990b4856Slling int
zprop_iter(zprop_func func,void * cb,boolean_t show_all,boolean_t ordered,zfs_type_t type)1629990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1630990b4856Slling     zfs_type_t type)
1631990b4856Slling {
1632990b4856Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1633ecd6cf80Smarks }
16342bcf0248SMax Grossman 
16352bcf0248SMax Grossman /*
16362bcf0248SMax Grossman  * zfs_get_hole_count retrieves the number of holes (blocks which are
16372bcf0248SMax Grossman  * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It
16382bcf0248SMax Grossman  * also optionally fetches the block size when bs is non-NULL. With hole count
16392bcf0248SMax Grossman  * and block size the full space consumed by the holes of a file can be
16402bcf0248SMax Grossman  * calculated.
16412bcf0248SMax Grossman  *
16422bcf0248SMax Grossman  * On success, zero is returned, the count argument is set to the
16432bcf0248SMax Grossman  * number of holes, and the bs argument is set to the block size (if it is
16442bcf0248SMax Grossman  * not NULL). On error, a non-zero errno is returned and the values in count
16452bcf0248SMax Grossman  * and bs are undefined.
16462bcf0248SMax Grossman  */
16472bcf0248SMax Grossman int
zfs_get_hole_count(const char * path,uint64_t * count,uint64_t * bs)16489a686fbcSPaul Dagnelie zfs_get_hole_count(const char *path, uint64_t *count, uint64_t *bs)
16499a686fbcSPaul Dagnelie {
16502bcf0248SMax Grossman 	int fd, err;
16512bcf0248SMax Grossman 	struct stat64 ss;
16522bcf0248SMax Grossman 	uint64_t fill;
16532bcf0248SMax Grossman 
16542bcf0248SMax Grossman 	fd = open(path, O_RDONLY | O_LARGEFILE);
16552bcf0248SMax Grossman 	if (fd == -1)
16562bcf0248SMax Grossman 		return (errno);
16572bcf0248SMax Grossman 
16582bcf0248SMax Grossman 	if (ioctl(fd, _FIO_COUNT_FILLED, &fill) == -1) {
16592bcf0248SMax Grossman 		err = errno;
16602bcf0248SMax Grossman 		(void) close(fd);
16612bcf0248SMax Grossman 		return (err);
16622bcf0248SMax Grossman 	}
16632bcf0248SMax Grossman 
16642bcf0248SMax Grossman 	if (fstat64(fd, &ss) == -1) {
16652bcf0248SMax Grossman 		err = errno;
16662bcf0248SMax Grossman 		(void) close(fd);
16672bcf0248SMax Grossman 		return (err);
16682bcf0248SMax Grossman 	}
16692bcf0248SMax Grossman 
16702bcf0248SMax Grossman 	*count = (ss.st_size + ss.st_blksize - 1) / ss.st_blksize - fill;
16712bcf0248SMax Grossman 	VERIFY3S(*count, >=, 0);
16722bcf0248SMax Grossman 	if (bs != NULL) {
16732bcf0248SMax Grossman 		*bs = ss.st_blksize;
16742bcf0248SMax Grossman 	}
16752bcf0248SMax Grossman 
16762bcf0248SMax Grossman 	if (close(fd) == -1) {
16772bcf0248SMax Grossman 		return (errno);
16782bcf0248SMax Grossman 	}
16792bcf0248SMax Grossman 	return (0);
16802bcf0248SMax Grossman }
1681