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.
24e0f1c0afSOlaf Faaland  * Copyright 2019 Joyent, Inc.
2586714001SSerapheim Dimitropoulos  * Copyright (c) 2011, 2017 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 
34fa9e4066Sahrens #include <errno.h>
35fa9e4066Sahrens #include <fcntl.h>
36fa9e4066Sahrens #include <libintl.h>
37fa9e4066Sahrens #include <stdarg.h>
38fa9e4066Sahrens #include <stdio.h>
39fa9e4066Sahrens #include <stdlib.h>
40fa9e4066Sahrens #include <strings.h>
41fa9e4066Sahrens #include <unistd.h>
42990b4856Slling #include <ctype.h>
43990b4856Slling #include <math.h>
442bcf0248SMax Grossman #include <sys/filio.h>
45fa9e4066Sahrens #include <sys/mnttab.h>
465aba80dbSck #include <sys/mntent.h>
475aba80dbSck #include <sys/types.h>
480a055120SJason King #include <libcmdutils.h>
49fa9e4066Sahrens 
50fa9e4066Sahrens #include <libzfs.h>
514445fffbSMatthew Ahrens #include <libzfs_core.h>
52fa9e4066Sahrens 
53fa9e4066Sahrens #include "libzfs_impl.h"
5491ebeef5Sahrens #include "zfs_prop.h"
55e0f1c0afSOlaf Faaland #include "zfs_comutil.h"
56ad135b5dSChristopher Siden #include "zfeature_common.h"
57fa9e4066Sahrens 
5899653d4eSeschrock int
5999653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
6099653d4eSeschrock {
6199653d4eSeschrock 	return (hdl->libzfs_error);
6299653d4eSeschrock }
63fa9e4066Sahrens 
6499653d4eSeschrock const char *
6599653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
6699653d4eSeschrock {
6799653d4eSeschrock 	return (hdl->libzfs_action);
6899653d4eSeschrock }
69fa9e4066Sahrens 
7099653d4eSeschrock const char *
7199653d4eSeschrock libzfs_error_description(libzfs_handle_t *hdl)
7299653d4eSeschrock {
7399653d4eSeschrock 	if (hdl->libzfs_desc[0] != '\0')
7499653d4eSeschrock 		return (hdl->libzfs_desc);
7599653d4eSeschrock 
7699653d4eSeschrock 	switch (hdl->libzfs_error) {
7799653d4eSeschrock 	case EZFS_NOMEM:
7899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of memory"));
7999653d4eSeschrock 	case EZFS_BADPROP:
8099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid property value"));
8199653d4eSeschrock 	case EZFS_PROPREADONLY:
82f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "read-only property"));
8399653d4eSeschrock 	case EZFS_PROPTYPE:
8499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
8599653d4eSeschrock 		    "datasets of this type"));
8699653d4eSeschrock 	case EZFS_PROPNONINHERIT:
8799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
8899653d4eSeschrock 	case EZFS_PROPSPACE:
8999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
9099653d4eSeschrock 	case EZFS_BADTYPE:
9199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
9299653d4eSeschrock 		    "datasets of this type"));
9399653d4eSeschrock 	case EZFS_BUSY:
9499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
9599653d4eSeschrock 	case EZFS_EXISTS:
9699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
9799653d4eSeschrock 	case EZFS_NOENT:
9899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
9999653d4eSeschrock 	case EZFS_BADSTREAM:
10099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
10199653d4eSeschrock 	case EZFS_DSREADONLY:
102f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
10399653d4eSeschrock 	case EZFS_VOLTOOBIG:
10499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
10599653d4eSeschrock 		    "this system"));
10699653d4eSeschrock 	case EZFS_INVALIDNAME:
10799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid name"));
10899653d4eSeschrock 	case EZFS_BADRESTORE:
10999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unable to restore to "
11099653d4eSeschrock 		    "destination"));
11199653d4eSeschrock 	case EZFS_BADBACKUP:
11299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "backup failed"));
11399653d4eSeschrock 	case EZFS_BADTARGET:
11499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
11599653d4eSeschrock 	case EZFS_NODEVICE:
11699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
11799653d4eSeschrock 	case EZFS_BADDEV:
11899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid device"));
11999653d4eSeschrock 	case EZFS_NOREPLICAS:
12099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
12199653d4eSeschrock 	case EZFS_RESILVERING:
12299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
12399653d4eSeschrock 	case EZFS_BADVERSION:
124ad135b5dSChristopher Siden 		return (dgettext(TEXT_DOMAIN, "unsupported version or "
125ad135b5dSChristopher Siden 		    "feature"));
12699653d4eSeschrock 	case EZFS_POOLUNAVAIL:
12799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
12899653d4eSeschrock 	case EZFS_DEVOVERFLOW:
12999653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
13099653d4eSeschrock 	case EZFS_BADPATH:
13199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
13299653d4eSeschrock 	case EZFS_CROSSTARGET:
13399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
13499653d4eSeschrock 		    "pools"));
13599653d4eSeschrock 	case EZFS_ZONED:
13699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
13799653d4eSeschrock 	case EZFS_MOUNTFAILED:
13899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "mount failed"));
13999653d4eSeschrock 	case EZFS_UMOUNTFAILED:
14099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "umount failed"));
141f3861e1aSahl 	case EZFS_UNSHARENFSFAILED:
14299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
143f3861e1aSahl 	case EZFS_SHARENFSFAILED:
14499653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
145da6c28aaSamw 	case EZFS_UNSHARESMBFAILED:
146da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
147da6c28aaSamw 	case EZFS_SHARESMBFAILED:
148da6c28aaSamw 		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
14999653d4eSeschrock 	case EZFS_PERM:
15099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "permission denied"));
15199653d4eSeschrock 	case EZFS_NOSPC:
15299653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "out of space"));
1536e27f868SSam Falkner 	case EZFS_FAULT:
1546e27f868SSam Falkner 		return (dgettext(TEXT_DOMAIN, "bad address"));
15599653d4eSeschrock 	case EZFS_IO:
15699653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "I/O error"));
15799653d4eSeschrock 	case EZFS_INTR:
15899653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "signal received"));
15999653d4eSeschrock 	case EZFS_ISSPARE:
16099653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
16199653d4eSeschrock 		    "spare"));
16299653d4eSeschrock 	case EZFS_INVALCONFIG:
16399653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1643bb79becSeschrock 	case EZFS_RECURSIVE:
1653bb79becSeschrock 		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
16606eeb2adSek 	case EZFS_NOHISTORY:
16706eeb2adSek 		return (dgettext(TEXT_DOMAIN, "no history available"));
168b1b8ab34Slling 	case EZFS_POOLPROPS:
169b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
170b1b8ab34Slling 		    "pool properties"));
171b1b8ab34Slling 	case EZFS_POOL_NOTSUP:
172b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "operation not supported "
173b1b8ab34Slling 		    "on this type of pool"));
174b1b8ab34Slling 	case EZFS_POOL_INVALARG:
175b1b8ab34Slling 		return (dgettext(TEXT_DOMAIN, "invalid argument for "
176b1b8ab34Slling 		    "this pool operation"));
177b7661cccSmmusante 	case EZFS_NAMETOOLONG:
178b7661cccSmmusante 		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1798488aeb5Staylor 	case EZFS_OPENFAILED:
1808488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "open failed"));
1818488aeb5Staylor 	case EZFS_NOCAP:
1828488aeb5Staylor 		return (dgettext(TEXT_DOMAIN,
1838488aeb5Staylor 		    "disk capacity information could not be retrieved"));
1848488aeb5Staylor 	case EZFS_LABELFAILED:
1858488aeb5Staylor 		return (dgettext(TEXT_DOMAIN, "write of label failed"));
186ecd6cf80Smarks 	case EZFS_BADWHO:
187ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
188ecd6cf80Smarks 	case EZFS_BADPERM:
189ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission"));
190ecd6cf80Smarks 	case EZFS_BADPERMSET:
191ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
192ecd6cf80Smarks 	case EZFS_NODELEGATION:
193ecd6cf80Smarks 		return (dgettext(TEXT_DOMAIN, "delegated administration is "
194ecd6cf80Smarks 		    "disabled on pool"));
1952f8aaab3Seschrock 	case EZFS_BADCACHE:
1962f8aaab3Seschrock 		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
197fa94a07fSbrendan 	case EZFS_ISL2CACHE:
198fa94a07fSbrendan 		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
199e7cbe64fSgw 	case EZFS_VDEVNOTSUP:
200e7cbe64fSgw 		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
201e7cbe64fSgw 		    "supported"));
20215e6edf1Sgw 	case EZFS_NOTSUP:
20315e6edf1Sgw 		return (dgettext(TEXT_DOMAIN, "operation not supported "
20415e6edf1Sgw 		    "on this dataset"));
20589a89ebfSlling 	case EZFS_ACTIVE_SPARE:
20689a89ebfSlling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
20789a89ebfSlling 		    "device"));
208e6ca193dSGeorge Wilson 	case EZFS_UNPLAYED_LOGS:
209e6ca193dSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
210e6ca193dSGeorge Wilson 		    "logs"));
211842727c2SChris Kirby 	case EZFS_REFTAG_RELE:
212842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
213842727c2SChris Kirby 	case EZFS_REFTAG_HOLD:
214842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
215842727c2SChris Kirby 		    "dataset"));
216ca45db41SChris Kirby 	case EZFS_TAGTOOLONG:
217ca45db41SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag too long"));
2189e69d7d0SLori Alt 	case EZFS_PIPEFAILED:
2199e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
2209e69d7d0SLori Alt 	case EZFS_THREADCREATEFAILED:
2219e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "thread create failed"));
2221195e687SMark J Musante 	case EZFS_POSTSPLIT_ONLINE:
2231195e687SMark J Musante 		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
2241195e687SMark J Musante 		    "into a new one"));
2251702cce7SAlek Pinchuk 	case EZFS_SCRUB_PAUSED:
2261702cce7SAlek Pinchuk 		return (dgettext(TEXT_DOMAIN, "scrub is paused; "
2271702cce7SAlek Pinchuk 		    "use 'zpool scrub' to resume"));
2283f9d6ad7SLin Ling 	case EZFS_SCRUBBING:
2293f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
2303f9d6ad7SLin Ling 		    "use 'zpool scrub -s' to cancel current scrub"));
2313f9d6ad7SLin Ling 	case EZFS_NO_SCRUB:
2323f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
23399d5e173STim Haley 	case EZFS_DIFF:
23499d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
23599d5e173STim Haley 	case EZFS_DIFFDATA:
23699d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
237f9af39baSGeorge Wilson 	case EZFS_POOLREADONLY:
238f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
2395cabbc6bSPrashanth Sreenivasa 	case EZFS_NO_PENDING:
2405cabbc6bSPrashanth Sreenivasa 		return (dgettext(TEXT_DOMAIN, "operation is not "
2415cabbc6bSPrashanth Sreenivasa 		    "in progress"));
24286714001SSerapheim Dimitropoulos 	case EZFS_CHECKPOINT_EXISTS:
24386714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
24486714001SSerapheim Dimitropoulos 	case EZFS_DISCARDING_CHECKPOINT:
24586714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "currently discarding "
24686714001SSerapheim Dimitropoulos 		    "checkpoint"));
24786714001SSerapheim Dimitropoulos 	case EZFS_NO_CHECKPOINT:
24886714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
24986714001SSerapheim Dimitropoulos 	case EZFS_DEVRM_IN_PROGRESS:
25086714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "device removal in progress"));
25186714001SSerapheim Dimitropoulos 	case EZFS_VDEV_TOO_BIG:
25286714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
253e0f1c0afSOlaf Faaland 	case EZFS_ACTIVE_POOL:
254e0f1c0afSOlaf Faaland 		return (dgettext(TEXT_DOMAIN, "pool is imported on a "
255e0f1c0afSOlaf Faaland 		    "different host"));
256094e47e9SGeorge Wilson 	case EZFS_TOOMANY:
257094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "argument list too long"));
258094e47e9SGeorge Wilson 	case EZFS_INITIALIZING:
259094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "currently initializing"));
260094e47e9SGeorge Wilson 	case EZFS_NO_INITIALIZE:
261094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "there is no active "
262094e47e9SGeorge Wilson 		    "initialization"));
263*e4c795beSTom Caputi 	case EZFS_NO_RESILVER_DEFER:
264*e4c795beSTom Caputi 		return (dgettext(TEXT_DOMAIN, "this action requires the "
265*e4c795beSTom Caputi 		    "resilver_defer feature"));
26699653d4eSeschrock 	case EZFS_UNKNOWN:
26799653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
26899653d4eSeschrock 	default:
269c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
270c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
27199653d4eSeschrock 	}
27299653d4eSeschrock }
27399653d4eSeschrock 
27499653d4eSeschrock /*PRINTFLIKE2*/
275fa9e4066Sahrens void
27699653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
277fa9e4066Sahrens {
278fa9e4066Sahrens 	va_list ap;
279fa9e4066Sahrens 
280fa9e4066Sahrens 	va_start(ap, fmt);
281fa9e4066Sahrens 
28299653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
28399653d4eSeschrock 	    fmt, ap);
28499653d4eSeschrock 	hdl->libzfs_desc_active = 1;
28599653d4eSeschrock 
28699653d4eSeschrock 	va_end(ap);
28799653d4eSeschrock }
28899653d4eSeschrock 
28999653d4eSeschrock static void
29099653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
29199653d4eSeschrock {
29299653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
29399653d4eSeschrock 	    fmt, ap);
29499653d4eSeschrock 	hdl->libzfs_error = error;
29599653d4eSeschrock 
29699653d4eSeschrock 	if (hdl->libzfs_desc_active)
29799653d4eSeschrock 		hdl->libzfs_desc_active = 0;
29899653d4eSeschrock 	else
29999653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
30099653d4eSeschrock 
30199653d4eSeschrock 	if (hdl->libzfs_printerr) {
30299653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
30399653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
30499653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
30599653d4eSeschrock 			abort();
30699653d4eSeschrock 		}
30799653d4eSeschrock 
30899653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
309b1b8ab34Slling 		    libzfs_error_description(hdl));
31099653d4eSeschrock 		if (error == EZFS_NOMEM)
31199653d4eSeschrock 			exit(1);
312fa9e4066Sahrens 	}
31399653d4eSeschrock }
31499653d4eSeschrock 
315ece3d9b3Slling int
316ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
317ece3d9b3Slling {
318ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
319ece3d9b3Slling }
320ece3d9b3Slling 
32199653d4eSeschrock /*PRINTFLIKE3*/
32299653d4eSeschrock int
323ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
32499653d4eSeschrock {
32599653d4eSeschrock 	va_list ap;
32699653d4eSeschrock 
32799653d4eSeschrock 	va_start(ap, fmt);
32899653d4eSeschrock 
32999653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
330fa9e4066Sahrens 
331fa9e4066Sahrens 	va_end(ap);
33299653d4eSeschrock 
33399653d4eSeschrock 	return (-1);
334fa9e4066Sahrens }
335fa9e4066Sahrens 
33699653d4eSeschrock static int
33799653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
33899653d4eSeschrock     va_list ap)
33999653d4eSeschrock {
34099653d4eSeschrock 	switch (error) {
34199653d4eSeschrock 	case EPERM:
34299653d4eSeschrock 	case EACCES:
34399653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
34499653d4eSeschrock 		return (-1);
34599653d4eSeschrock 
346ecd6cf80Smarks 	case ECANCELED:
347ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
348ecd6cf80Smarks 		return (-1);
349ecd6cf80Smarks 
35099653d4eSeschrock 	case EIO:
35199653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
35299653d4eSeschrock 		return (-1);
35399653d4eSeschrock 
3546e27f868SSam Falkner 	case EFAULT:
3556e27f868SSam Falkner 		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
3566e27f868SSam Falkner 		return (-1);
3576e27f868SSam Falkner 
35899653d4eSeschrock 	case EINTR:
35999653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
36099653d4eSeschrock 		return (-1);
36199653d4eSeschrock 	}
36299653d4eSeschrock 
36399653d4eSeschrock 	return (0);
36499653d4eSeschrock }
36599653d4eSeschrock 
366ece3d9b3Slling int
367ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
368ece3d9b3Slling {
369ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
370ece3d9b3Slling }
371ece3d9b3Slling 
37299653d4eSeschrock /*PRINTFLIKE3*/
37399653d4eSeschrock int
374ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
375fa9e4066Sahrens {
376fa9e4066Sahrens 	va_list ap;
377fa9e4066Sahrens 
378fa9e4066Sahrens 	va_start(ap, fmt);
379fa9e4066Sahrens 
38099653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
38199653d4eSeschrock 		va_end(ap);
38299653d4eSeschrock 		return (-1);
383fa9e4066Sahrens 	}
384fa9e4066Sahrens 
38599653d4eSeschrock 	switch (error) {
38699653d4eSeschrock 	case ENXIO:
38797d9e3a6Sck 	case ENODEV:
38819b94df9SMatthew Ahrens 	case EPIPE:
38999653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
39099653d4eSeschrock 		break;
39199653d4eSeschrock 
39299653d4eSeschrock 	case ENOENT:
39399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39499653d4eSeschrock 		    "dataset does not exist"));
39599653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
39699653d4eSeschrock 		break;
39799653d4eSeschrock 
39899653d4eSeschrock 	case ENOSPC:
39999653d4eSeschrock 	case EDQUOT:
40099653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
40199653d4eSeschrock 		return (-1);
40299653d4eSeschrock 
40399653d4eSeschrock 	case EEXIST:
40499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40599653d4eSeschrock 		    "dataset already exists"));
40699653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
40799653d4eSeschrock 		break;
40899653d4eSeschrock 
40999653d4eSeschrock 	case EBUSY:
41099653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41199653d4eSeschrock 		    "dataset is busy"));
41299653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
41399653d4eSeschrock 		break;
414ecd6cf80Smarks 	case EROFS:
415f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
416ecd6cf80Smarks 		break;
417b7661cccSmmusante 	case ENAMETOOLONG:
418b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
419b7661cccSmmusante 		break;
42040ff3960Sck 	case ENOTSUP:
42140ff3960Sck 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
42240ff3960Sck 		break;
42354d692b7SGeorge Wilson 	case EAGAIN:
42454d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42554d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
42654d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
42754d692b7SGeorge Wilson 		break;
428e0f1c0afSOlaf Faaland 	case EREMOTEIO:
429e0f1c0afSOlaf Faaland 		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
430e0f1c0afSOlaf Faaland 		break;
43199653d4eSeschrock 	default:
43292241e0bSTom Erickson 		zfs_error_aux(hdl, strerror(error));
43399653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
43499653d4eSeschrock 		break;
43599653d4eSeschrock 	}
43699653d4eSeschrock 
43799653d4eSeschrock 	va_end(ap);
43899653d4eSeschrock 	return (-1);
439fa9e4066Sahrens }
440fa9e4066Sahrens 
441ece3d9b3Slling int
442ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
443ece3d9b3Slling {
444ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
445ece3d9b3Slling }
446ece3d9b3Slling 
44799653d4eSeschrock /*PRINTFLIKE3*/
44899653d4eSeschrock int
449ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
450fa9e4066Sahrens {
45199653d4eSeschrock 	va_list ap;
45299653d4eSeschrock 
45399653d4eSeschrock 	va_start(ap, fmt);
45499653d4eSeschrock 
45599653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
45699653d4eSeschrock 		va_end(ap);
45799653d4eSeschrock 		return (-1);
45899653d4eSeschrock 	}
45999653d4eSeschrock 
46099653d4eSeschrock 	switch (error) {
46199653d4eSeschrock 	case ENODEV:
46299653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
46399653d4eSeschrock 		break;
46499653d4eSeschrock 
46599653d4eSeschrock 	case ENOENT:
466b1b8ab34Slling 		zfs_error_aux(hdl,
467b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
46899653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
46999653d4eSeschrock 		break;
47099653d4eSeschrock 
47199653d4eSeschrock 	case EEXIST:
47299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
47399653d4eSeschrock 		    "pool already exists"));
47499653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
47599653d4eSeschrock 		break;
47699653d4eSeschrock 
47799653d4eSeschrock 	case EBUSY:
47899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
47943afaaa8SEric Schrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
48099653d4eSeschrock 		break;
48199653d4eSeschrock 
48299653d4eSeschrock 	case ENXIO:
48399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
48499653d4eSeschrock 		    "one or more devices is currently unavailable"));
48599653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
48699653d4eSeschrock 		break;
48799653d4eSeschrock 
48899653d4eSeschrock 	case ENAMETOOLONG:
48999653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
49099653d4eSeschrock 		break;
49199653d4eSeschrock 
492b1b8ab34Slling 	case ENOTSUP:
493b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
494b1b8ab34Slling 		break;
495b1b8ab34Slling 
496b1b8ab34Slling 	case EINVAL:
497b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
498b1b8ab34Slling 		break;
499b1b8ab34Slling 
500ecd6cf80Smarks 	case ENOSPC:
501ecd6cf80Smarks 	case EDQUOT:
502ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
503ecd6cf80Smarks 		return (-1);
504f9af39baSGeorge Wilson 
50554d692b7SGeorge Wilson 	case EAGAIN:
50654d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50754d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
50854d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
50954d692b7SGeorge Wilson 		break;
510ecd6cf80Smarks 
511f9af39baSGeorge Wilson 	case EROFS:
512f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
513f9af39baSGeorge Wilson 		break;
5145cabbc6bSPrashanth Sreenivasa 	/* There is no pending operation to cancel */
5155cabbc6bSPrashanth Sreenivasa 	case ENOTACTIVE:
5165cabbc6bSPrashanth Sreenivasa 		zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
5175cabbc6bSPrashanth Sreenivasa 		break;
518e0f1c0afSOlaf Faaland 	case EREMOTEIO:
519e0f1c0afSOlaf Faaland 		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
520e0f1c0afSOlaf Faaland 		break;
52186714001SSerapheim Dimitropoulos 	case ZFS_ERR_CHECKPOINT_EXISTS:
52286714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
52386714001SSerapheim Dimitropoulos 		break;
52486714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
52586714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
52686714001SSerapheim Dimitropoulos 		break;
52786714001SSerapheim Dimitropoulos 	case ZFS_ERR_NO_CHECKPOINT:
52886714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
52986714001SSerapheim Dimitropoulos 		break;
53086714001SSerapheim Dimitropoulos 	case ZFS_ERR_DEVRM_IN_PROGRESS:
53186714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
53286714001SSerapheim Dimitropoulos 		break;
53386714001SSerapheim Dimitropoulos 	case ZFS_ERR_VDEV_TOO_BIG:
53486714001SSerapheim Dimitropoulos 		zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
53586714001SSerapheim Dimitropoulos 		break;
53699653d4eSeschrock 	default:
53799653d4eSeschrock 		zfs_error_aux(hdl, strerror(error));
53899653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
53999653d4eSeschrock 	}
54099653d4eSeschrock 
54199653d4eSeschrock 	va_end(ap);
54299653d4eSeschrock 	return (-1);
543fa9e4066Sahrens }
544fa9e4066Sahrens 
545fa9e4066Sahrens /*
546fa9e4066Sahrens  * Display an out of memory error message and abort the current program.
547fa9e4066Sahrens  */
54899653d4eSeschrock int
54999653d4eSeschrock no_memory(libzfs_handle_t *hdl)
550fa9e4066Sahrens {
55199653d4eSeschrock 	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
552fa9e4066Sahrens }
553fa9e4066Sahrens 
554fa9e4066Sahrens /*
555fa9e4066Sahrens  * A safe form of malloc() which will die if the allocation fails.
556fa9e4066Sahrens  */
557fa9e4066Sahrens void *
55899653d4eSeschrock zfs_alloc(libzfs_handle_t *hdl, size_t size)
559fa9e4066Sahrens {
560fa9e4066Sahrens 	void *data;
561fa9e4066Sahrens 
562fa9e4066Sahrens 	if ((data = calloc(1, size)) == NULL)
56399653d4eSeschrock 		(void) no_memory(hdl);
564fa9e4066Sahrens 
565fa9e4066Sahrens 	return (data);
566fa9e4066Sahrens }
567fa9e4066Sahrens 
56899d5e173STim Haley /*
56999d5e173STim Haley  * A safe form of asprintf() which will die if the allocation fails.
57099d5e173STim Haley  */
57199d5e173STim Haley /*PRINTFLIKE2*/
57299d5e173STim Haley char *
57399d5e173STim Haley zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
57499d5e173STim Haley {
57599d5e173STim Haley 	va_list ap;
57699d5e173STim Haley 	char *ret;
57799d5e173STim Haley 	int err;
57899d5e173STim Haley 
57999d5e173STim Haley 	va_start(ap, fmt);
58099d5e173STim Haley 
58199d5e173STim Haley 	err = vasprintf(&ret, fmt, ap);
58299d5e173STim Haley 
58399d5e173STim Haley 	va_end(ap);
58499d5e173STim Haley 
58599d5e173STim Haley 	if (err < 0)
58699d5e173STim Haley 		(void) no_memory(hdl);
58799d5e173STim Haley 
58899d5e173STim Haley 	return (ret);
58999d5e173STim Haley }
59099d5e173STim Haley 
591e9dbad6fSeschrock /*
592e9dbad6fSeschrock  * A safe form of realloc(), which also zeroes newly allocated space.
593e9dbad6fSeschrock  */
594e9dbad6fSeschrock void *
595e9dbad6fSeschrock zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
596e9dbad6fSeschrock {
597e9dbad6fSeschrock 	void *ret;
598e9dbad6fSeschrock 
599e9dbad6fSeschrock 	if ((ret = realloc(ptr, newsize)) == NULL) {
600e9dbad6fSeschrock 		(void) no_memory(hdl);
601e9dbad6fSeschrock 		return (NULL);
602e9dbad6fSeschrock 	}
603e9dbad6fSeschrock 
604e9dbad6fSeschrock 	bzero((char *)ret + oldsize, (newsize - oldsize));
605e9dbad6fSeschrock 	return (ret);
606e9dbad6fSeschrock }
607e9dbad6fSeschrock 
608fa9e4066Sahrens /*
609fa9e4066Sahrens  * A safe form of strdup() which will die if the allocation fails.
610fa9e4066Sahrens  */
611fa9e4066Sahrens char *
61299653d4eSeschrock zfs_strdup(libzfs_handle_t *hdl, const char *str)
613fa9e4066Sahrens {
614fa9e4066Sahrens 	char *ret;
615fa9e4066Sahrens 
616fa9e4066Sahrens 	if ((ret = strdup(str)) == NULL)
61799653d4eSeschrock 		(void) no_memory(hdl);
618fa9e4066Sahrens 
619fa9e4066Sahrens 	return (ret);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
622fa9e4066Sahrens /*
623fa9e4066Sahrens  * Convert a number to an appropriately human-readable output.
624fa9e4066Sahrens  */
625fa9e4066Sahrens void
626fa9e4066Sahrens zfs_nicenum(uint64_t num, char *buf, size_t buflen)
627fa9e4066Sahrens {
6280a055120SJason King 	nicenum(num, buf, buflen);
629fa9e4066Sahrens }
63099653d4eSeschrock 
63199653d4eSeschrock void
63299653d4eSeschrock libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr)
63399653d4eSeschrock {
63499653d4eSeschrock 	hdl->libzfs_printerr = printerr;
63599653d4eSeschrock }
63699653d4eSeschrock 
63799653d4eSeschrock libzfs_handle_t *
63899653d4eSeschrock libzfs_init(void)
63999653d4eSeschrock {
64099653d4eSeschrock 	libzfs_handle_t *hdl;
64199653d4eSeschrock 
64299d5e173STim Haley 	if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) {
64399653d4eSeschrock 		return (NULL);
64499653d4eSeschrock 	}
64599653d4eSeschrock 
646c08432ebSeschrock 	if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR)) < 0) {
64799653d4eSeschrock 		free(hdl);
64899653d4eSeschrock 		return (NULL);
64999653d4eSeschrock 	}
65099653d4eSeschrock 
651bde3d612SSimon Klinkert 	if ((hdl->libzfs_mnttab = fopen(MNTTAB, "rF")) == NULL) {
65299653d4eSeschrock 		(void) close(hdl->libzfs_fd);
65399653d4eSeschrock 		free(hdl);
65499653d4eSeschrock 		return (NULL);
65599653d4eSeschrock 	}
65699653d4eSeschrock 
657bde3d612SSimon Klinkert 	hdl->libzfs_sharetab = fopen("/etc/dfs/sharetab", "rF");
65899653d4eSeschrock 
6594445fffbSMatthew Ahrens 	if (libzfs_core_init() != 0) {
6604445fffbSMatthew Ahrens 		(void) close(hdl->libzfs_fd);
6614445fffbSMatthew Ahrens 		(void) fclose(hdl->libzfs_mnttab);
6624445fffbSMatthew Ahrens 		(void) fclose(hdl->libzfs_sharetab);
6634445fffbSMatthew Ahrens 		free(hdl);
6644445fffbSMatthew Ahrens 		return (NULL);
6654445fffbSMatthew Ahrens 	}
6664445fffbSMatthew Ahrens 
66791ebeef5Sahrens 	zfs_prop_init();
668990b4856Slling 	zpool_prop_init();
669ad135b5dSChristopher Siden 	zpool_feature_init();
670b2634b9cSEric Taylor 	libzfs_mnttab_init(hdl);
67191ebeef5Sahrens 
672dfc11533SChris Williamson 	if (getenv("ZFS_PROP_DEBUG") != NULL) {
673dfc11533SChris Williamson 		hdl->libzfs_prop_debug = B_TRUE;
674dfc11533SChris Williamson 	}
675dfc11533SChris Williamson 
67699653d4eSeschrock 	return (hdl);
67799653d4eSeschrock }
67899653d4eSeschrock 
67999653d4eSeschrock void
68099653d4eSeschrock libzfs_fini(libzfs_handle_t *hdl)
68199653d4eSeschrock {
68299653d4eSeschrock 	(void) close(hdl->libzfs_fd);
68399653d4eSeschrock 	if (hdl->libzfs_mnttab)
68499653d4eSeschrock 		(void) fclose(hdl->libzfs_mnttab);
68599653d4eSeschrock 	if (hdl->libzfs_sharetab)
68699653d4eSeschrock 		(void) fclose(hdl->libzfs_sharetab);
68767331909Sdougm 	zfs_uninit_libshare(hdl);
68829ab75c9Srm 	zpool_free_handles(hdl);
689069f55e2SEric Schrock 	libzfs_fru_clear(hdl, B_TRUE);
69099653d4eSeschrock 	namespace_clear(hdl);
691b2634b9cSEric Taylor 	libzfs_mnttab_fini(hdl);
6924445fffbSMatthew Ahrens 	libzfs_core_fini();
69399653d4eSeschrock 	free(hdl);
69499653d4eSeschrock }
69599653d4eSeschrock 
69699653d4eSeschrock libzfs_handle_t *
69799653d4eSeschrock zpool_get_handle(zpool_handle_t *zhp)
69899653d4eSeschrock {
69999653d4eSeschrock 	return (zhp->zpool_hdl);
70099653d4eSeschrock }
70199653d4eSeschrock 
70299653d4eSeschrock libzfs_handle_t *
70399653d4eSeschrock zfs_get_handle(zfs_handle_t *zhp)
70499653d4eSeschrock {
70599653d4eSeschrock 	return (zhp->zfs_hdl);
70699653d4eSeschrock }
707e9dbad6fSeschrock 
708d5b5bb25SRich Morris zpool_handle_t *
709d5b5bb25SRich Morris zfs_get_pool_handle(const zfs_handle_t *zhp)
710d5b5bb25SRich Morris {
711d5b5bb25SRich Morris 	return (zhp->zpool_hdl);
712d5b5bb25SRich Morris }
713d5b5bb25SRich Morris 
7145aba80dbSck /*
7155aba80dbSck  * Given a name, determine whether or not it's a valid path
7165aba80dbSck  * (starts with '/' or "./").  If so, walk the mnttab trying
7175aba80dbSck  * to match the device number.  If not, treat the path as an
718edb901aaSMarcel Telka  * fs/vol/snap/bkmark name.
7195aba80dbSck  */
7205aba80dbSck zfs_handle_t *
7215aba80dbSck zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
7225aba80dbSck {
7235aba80dbSck 	struct stat64 statbuf;
7245aba80dbSck 	struct extmnttab entry;
7255aba80dbSck 	int ret;
7265aba80dbSck 
7275aba80dbSck 	if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) {
7285aba80dbSck 		/*
7295aba80dbSck 		 * It's not a valid path, assume it's a name of type 'argtype'.
7305aba80dbSck 		 */
7315aba80dbSck 		return (zfs_open(hdl, path, argtype));
7325aba80dbSck 	}
7335aba80dbSck 
7345aba80dbSck 	if (stat64(path, &statbuf) != 0) {
7355aba80dbSck 		(void) fprintf(stderr, "%s: %s\n", path, strerror(errno));
7365aba80dbSck 		return (NULL);
7375aba80dbSck 	}
7385aba80dbSck 
7395aba80dbSck 	rewind(hdl->libzfs_mnttab);
7405aba80dbSck 	while ((ret = getextmntent(hdl->libzfs_mnttab, &entry, 0)) == 0) {
7415aba80dbSck 		if (makedevice(entry.mnt_major, entry.mnt_minor) ==
7425aba80dbSck 		    statbuf.st_dev) {
7435aba80dbSck 			break;
7445aba80dbSck 		}
7455aba80dbSck 	}
7465aba80dbSck 	if (ret != 0) {
7475aba80dbSck 		return (NULL);
7485aba80dbSck 	}
7495aba80dbSck 
7505aba80dbSck 	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
7515aba80dbSck 		(void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"),
7525aba80dbSck 		    path);
7535aba80dbSck 		return (NULL);
7545aba80dbSck 	}
7555aba80dbSck 
7565aba80dbSck 	return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM));
7575aba80dbSck }
7585aba80dbSck 
759e9dbad6fSeschrock /*
760e9dbad6fSeschrock  * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from
761e9dbad6fSeschrock  * an ioctl().
762e9dbad6fSeschrock  */
763e9dbad6fSeschrock int
764e9dbad6fSeschrock zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len)
765e9dbad6fSeschrock {
766e9dbad6fSeschrock 	if (len == 0)
7674b964adaSGeorge Wilson 		len = 16 * 1024;
768e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = len;
76930925561SChris Williamson 	zc->zc_nvlist_dst =
77030925561SChris Williamson 	    (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
77130925561SChris Williamson 	if (zc->zc_nvlist_dst == 0)
772e9dbad6fSeschrock 		return (-1);
773e9dbad6fSeschrock 
774e9dbad6fSeschrock 	return (0);
775e9dbad6fSeschrock }
776e9dbad6fSeschrock 
777e9dbad6fSeschrock /*
778e9dbad6fSeschrock  * Called when an ioctl() which returns an nvlist fails with ENOMEM.  This will
779e9dbad6fSeschrock  * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was
780e9dbad6fSeschrock  * filled in by the kernel to indicate the actual required size.
781e9dbad6fSeschrock  */
782e9dbad6fSeschrock int
783e9dbad6fSeschrock zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc)
784e9dbad6fSeschrock {
785e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
78630925561SChris Williamson 	zc->zc_nvlist_dst =
78730925561SChris Williamson 	    (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size);
78830925561SChris Williamson 	if (zc->zc_nvlist_dst == 0)
789e9dbad6fSeschrock 		return (-1);
790e9dbad6fSeschrock 
791e9dbad6fSeschrock 	return (0);
792e9dbad6fSeschrock }
793e9dbad6fSeschrock 
794e9dbad6fSeschrock /*
795a2eea2e1Sahrens  * Called to free the src and dst nvlists stored in the command structure.
796e9dbad6fSeschrock  */
797e9dbad6fSeschrock void
798e9dbad6fSeschrock zcmd_free_nvlists(zfs_cmd_t *zc)
799e9dbad6fSeschrock {
800990b4856Slling 	free((void *)(uintptr_t)zc->zc_nvlist_conf);
801e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_src);
802e9dbad6fSeschrock 	free((void *)(uintptr_t)zc->zc_nvlist_dst);
80330925561SChris Williamson 	zc->zc_nvlist_conf = NULL;
80430925561SChris Williamson 	zc->zc_nvlist_src = NULL;
80530925561SChris Williamson 	zc->zc_nvlist_dst = NULL;
806e9dbad6fSeschrock }
807e9dbad6fSeschrock 
808990b4856Slling static int
809990b4856Slling zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen,
810990b4856Slling     nvlist_t *nvl)
811e9dbad6fSeschrock {
812e9dbad6fSeschrock 	char *packed;
813e9dbad6fSeschrock 	size_t len;
814e9dbad6fSeschrock 
815e9dbad6fSeschrock 	verify(nvlist_size(nvl, &len, NV_ENCODE_NATIVE) == 0);
816e9dbad6fSeschrock 
817e9dbad6fSeschrock 	if ((packed = zfs_alloc(hdl, len)) == NULL)
818e9dbad6fSeschrock 		return (-1);
819e9dbad6fSeschrock 
820e9dbad6fSeschrock 	verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0);
821e9dbad6fSeschrock 
822990b4856Slling 	*outnv = (uint64_t)(uintptr_t)packed;
823990b4856Slling 	*outlen = len;
824e9dbad6fSeschrock 
825e9dbad6fSeschrock 	return (0);
826e9dbad6fSeschrock }
827e9dbad6fSeschrock 
828990b4856Slling int
829990b4856Slling zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
830990b4856Slling {
831990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf,
832990b4856Slling 	    &zc->zc_nvlist_conf_size, nvl));
833990b4856Slling }
834990b4856Slling 
835990b4856Slling int
836990b4856Slling zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl)
837990b4856Slling {
838990b4856Slling 	return (zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src,
839990b4856Slling 	    &zc->zc_nvlist_src_size, nvl));
840990b4856Slling }
841990b4856Slling 
842e9dbad6fSeschrock /*
843e9dbad6fSeschrock  * Unpacks an nvlist from the ZFS ioctl command structure.
844e9dbad6fSeschrock  */
845e9dbad6fSeschrock int
846e9dbad6fSeschrock zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp)
847e9dbad6fSeschrock {
848e9dbad6fSeschrock 	if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst,
849e9dbad6fSeschrock 	    zc->zc_nvlist_dst_size, nvlp, 0) != 0)
850e9dbad6fSeschrock 		return (no_memory(hdl));
851e9dbad6fSeschrock 
852e9dbad6fSeschrock 	return (0);
853e9dbad6fSeschrock }
854b1b8ab34Slling 
855990b4856Slling int
856990b4856Slling zfs_ioctl(libzfs_handle_t *hdl, int request, zfs_cmd_t *zc)
857990b4856Slling {
8584445fffbSMatthew Ahrens 	return (ioctl(hdl->libzfs_fd, request, zc));
859990b4856Slling }
860990b4856Slling 
861990b4856Slling /*
862990b4856Slling  * ================================================================
863990b4856Slling  * API shared by zfs and zpool property management
864990b4856Slling  * ================================================================
865990b4856Slling  */
866990b4856Slling 
867b1b8ab34Slling static void
868990b4856Slling zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type)
869b1b8ab34Slling {
870990b4856Slling 	zprop_list_t *pl = cbp->cb_proplist;
871b1b8ab34Slling 	int i;
872b1b8ab34Slling 	char *title;
873b1b8ab34Slling 	size_t len;
874b1b8ab34Slling 
875b1b8ab34Slling 	cbp->cb_first = B_FALSE;
876b1b8ab34Slling 	if (cbp->cb_scripted)
877b1b8ab34Slling 		return;
878b1b8ab34Slling 
879b1b8ab34Slling 	/*
880b1b8ab34Slling 	 * Start with the length of the column headers.
881b1b8ab34Slling 	 */
882b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME"));
883b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN,
884b1b8ab34Slling 	    "PROPERTY"));
885b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN,
886b1b8ab34Slling 	    "VALUE"));
88792241e0bSTom Erickson 	cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN,
88892241e0bSTom Erickson 	    "RECEIVED"));
889b1b8ab34Slling 	cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN,
890b1b8ab34Slling 	    "SOURCE"));
891b1b8ab34Slling 
892deb8317bSMark J Musante 	/* first property is always NAME */
893deb8317bSMark J Musante 	assert(cbp->cb_proplist->pl_prop ==
894deb8317bSMark J Musante 	    ((type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME : ZFS_PROP_NAME));
895deb8317bSMark J Musante 
896b1b8ab34Slling 	/*
897b1b8ab34Slling 	 * Go through and calculate the widths for each column.  For the
898b1b8ab34Slling 	 * 'source' column, we kludge it up by taking the worst-case scenario of
899b1b8ab34Slling 	 * inheriting from the longest name.  This is acceptable because in the
900b1b8ab34Slling 	 * majority of cases 'SOURCE' is the last column displayed, and we don't
901b1b8ab34Slling 	 * use the width anyway.  Note that the 'VALUE' column can be oversized,
90292241e0bSTom Erickson 	 * if the name of the property is much longer than any values we find.
903b1b8ab34Slling 	 */
904b1b8ab34Slling 	for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) {
905b1b8ab34Slling 		/*
906b1b8ab34Slling 		 * 'PROPERTY' column
907b1b8ab34Slling 		 */
908990b4856Slling 		if (pl->pl_prop != ZPROP_INVAL) {
909990b4856Slling 			const char *propname = (type == ZFS_TYPE_POOL) ?
910990b4856Slling 			    zpool_prop_to_name(pl->pl_prop) :
911990b4856Slling 			    zfs_prop_to_name(pl->pl_prop);
912990b4856Slling 
913990b4856Slling 			len = strlen(propname);
914b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
915b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
916b1b8ab34Slling 		} else {
917b1b8ab34Slling 			len = strlen(pl->pl_user_prop);
918b1b8ab34Slling 			if (len > cbp->cb_colwidths[GET_COL_PROPERTY])
919b1b8ab34Slling 				cbp->cb_colwidths[GET_COL_PROPERTY] = len;
920b1b8ab34Slling 		}
921b1b8ab34Slling 
922b1b8ab34Slling 		/*
923deb8317bSMark J Musante 		 * 'VALUE' column.  The first property is always the 'name'
924deb8317bSMark J Musante 		 * property that was tacked on either by /sbin/zfs's
925deb8317bSMark J Musante 		 * zfs_do_get() or when calling zprop_expand_list(), so we
926deb8317bSMark J Musante 		 * ignore its width.  If the user specified the name property
927deb8317bSMark J Musante 		 * to display, then it will be later in the list in any case.
928b1b8ab34Slling 		 */
929deb8317bSMark J Musante 		if (pl != cbp->cb_proplist &&
930b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE])
931b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width;
932b1b8ab34Slling 
93392241e0bSTom Erickson 		/* 'RECEIVED' column. */
93492241e0bSTom Erickson 		if (pl != cbp->cb_proplist &&
93592241e0bSTom Erickson 		    pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD])
93692241e0bSTom Erickson 			cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width;
93792241e0bSTom Erickson 
938b1b8ab34Slling 		/*
939b1b8ab34Slling 		 * 'NAME' and 'SOURCE' columns
940b1b8ab34Slling 		 */
941990b4856Slling 		if (pl->pl_prop == (type == ZFS_TYPE_POOL ? ZPOOL_PROP_NAME :
942990b4856Slling 		    ZFS_PROP_NAME) &&
943b1b8ab34Slling 		    pl->pl_width > cbp->cb_colwidths[GET_COL_NAME]) {
944b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width;
945b1b8ab34Slling 			cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width +
946b1b8ab34Slling 			    strlen(dgettext(TEXT_DOMAIN, "inherited from"));
947b1b8ab34Slling 		}
948b1b8ab34Slling 	}
949b1b8ab34Slling 
950b1b8ab34Slling 	/*
951b1b8ab34Slling 	 * Now go through and print the headers.
952b1b8ab34Slling 	 */
95392241e0bSTom Erickson 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
954b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
955b1b8ab34Slling 		case GET_COL_NAME:
956b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "NAME");
957b1b8ab34Slling 			break;
958b1b8ab34Slling 		case GET_COL_PROPERTY:
959b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "PROPERTY");
960b1b8ab34Slling 			break;
961b1b8ab34Slling 		case GET_COL_VALUE:
962b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "VALUE");
963b1b8ab34Slling 			break;
96492241e0bSTom Erickson 		case GET_COL_RECVD:
96592241e0bSTom Erickson 			title = dgettext(TEXT_DOMAIN, "RECEIVED");
96692241e0bSTom Erickson 			break;
967b1b8ab34Slling 		case GET_COL_SOURCE:
968b1b8ab34Slling 			title = dgettext(TEXT_DOMAIN, "SOURCE");
969b1b8ab34Slling 			break;
970b1b8ab34Slling 		default:
971b1b8ab34Slling 			title = NULL;
972b1b8ab34Slling 		}
973b1b8ab34Slling 
974b1b8ab34Slling 		if (title != NULL) {
97592241e0bSTom Erickson 			if (i == (ZFS_GET_NCOLS - 1) ||
97692241e0bSTom Erickson 			    cbp->cb_columns[i + 1] == GET_COL_NONE)
977b1b8ab34Slling 				(void) printf("%s", title);
978b1b8ab34Slling 			else
979b1b8ab34Slling 				(void) printf("%-*s  ",
980b1b8ab34Slling 				    cbp->cb_colwidths[cbp->cb_columns[i]],
981b1b8ab34Slling 				    title);
982b1b8ab34Slling 		}
983b1b8ab34Slling 	}
984b1b8ab34Slling 	(void) printf("\n");
985b1b8ab34Slling }
986b1b8ab34Slling 
987b1b8ab34Slling /*
988b1b8ab34Slling  * Display a single line of output, according to the settings in the callback
989b1b8ab34Slling  * structure.
990b1b8ab34Slling  */
991b1b8ab34Slling void
992990b4856Slling zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp,
993990b4856Slling     const char *propname, const char *value, zprop_source_t sourcetype,
99492241e0bSTom Erickson     const char *source, const char *recvd_value)
995b1b8ab34Slling {
996b1b8ab34Slling 	int i;
997f83b46baSPaul Dagnelie 	const char *str = NULL;
998b1b8ab34Slling 	char buf[128];
999b1b8ab34Slling 
1000b1b8ab34Slling 	/*
1001b1b8ab34Slling 	 * Ignore those source types that the user has chosen to ignore.
1002b1b8ab34Slling 	 */
1003b1b8ab34Slling 	if ((sourcetype & cbp->cb_sources) == 0)
1004b1b8ab34Slling 		return;
1005b1b8ab34Slling 
1006b1b8ab34Slling 	if (cbp->cb_first)
1007990b4856Slling 		zprop_print_headers(cbp, cbp->cb_type);
1008b1b8ab34Slling 
100992241e0bSTom Erickson 	for (i = 0; i < ZFS_GET_NCOLS; i++) {
1010b1b8ab34Slling 		switch (cbp->cb_columns[i]) {
1011b1b8ab34Slling 		case GET_COL_NAME:
1012b1b8ab34Slling 			str = name;
1013b1b8ab34Slling 			break;
1014b1b8ab34Slling 
1015b1b8ab34Slling 		case GET_COL_PROPERTY:
1016b1b8ab34Slling 			str = propname;
1017b1b8ab34Slling 			break;
1018b1b8ab34Slling 
1019b1b8ab34Slling 		case GET_COL_VALUE:
1020b1b8ab34Slling 			str = value;
1021b1b8ab34Slling 			break;
1022b1b8ab34Slling 
1023b1b8ab34Slling 		case GET_COL_SOURCE:
1024b1b8ab34Slling 			switch (sourcetype) {
1025990b4856Slling 			case ZPROP_SRC_NONE:
1026b1b8ab34Slling 				str = "-";
1027b1b8ab34Slling 				break;
1028b1b8ab34Slling 
1029990b4856Slling 			case ZPROP_SRC_DEFAULT:
1030b1b8ab34Slling 				str = "default";
1031b1b8ab34Slling 				break;
1032b1b8ab34Slling 
1033990b4856Slling 			case ZPROP_SRC_LOCAL:
1034b1b8ab34Slling 				str = "local";
1035b1b8ab34Slling 				break;
1036b1b8ab34Slling 
1037990b4856Slling 			case ZPROP_SRC_TEMPORARY:
1038b1b8ab34Slling 				str = "temporary";
1039b1b8ab34Slling 				break;
1040b1b8ab34Slling 
1041990b4856Slling 			case ZPROP_SRC_INHERITED:
1042b1b8ab34Slling 				(void) snprintf(buf, sizeof (buf),
1043b1b8ab34Slling 				    "inherited from %s", source);
1044b1b8ab34Slling 				str = buf;
1045b1b8ab34Slling 				break;
104692241e0bSTom Erickson 			case ZPROP_SRC_RECEIVED:
104792241e0bSTom Erickson 				str = "received";
104892241e0bSTom Erickson 				break;
104988f61deeSIgor Kozhukhov 
105088f61deeSIgor Kozhukhov 			default:
105188f61deeSIgor Kozhukhov 				str = NULL;
105288f61deeSIgor Kozhukhov 				assert(!"unhandled zprop_source_t");
1053b1b8ab34Slling 			}
1054b1b8ab34Slling 			break;
1055b1b8ab34Slling 
105692241e0bSTom Erickson 		case GET_COL_RECVD:
105792241e0bSTom Erickson 			str = (recvd_value == NULL ? "-" : recvd_value);
105892241e0bSTom Erickson 			break;
105992241e0bSTom Erickson 
1060b1b8ab34Slling 		default:
1061b1b8ab34Slling 			continue;
1062b1b8ab34Slling 		}
1063b1b8ab34Slling 
106492241e0bSTom Erickson 		if (cbp->cb_columns[i + 1] == GET_COL_NONE)
1065b1b8ab34Slling 			(void) printf("%s", str);
1066b1b8ab34Slling 		else if (cbp->cb_scripted)
1067b1b8ab34Slling 			(void) printf("%s\t", str);
1068b1b8ab34Slling 		else
1069b1b8ab34Slling 			(void) printf("%-*s  ",
1070b1b8ab34Slling 			    cbp->cb_colwidths[cbp->cb_columns[i]],
1071b1b8ab34Slling 			    str);
1072b1b8ab34Slling 	}
1073b1b8ab34Slling 
1074b1b8ab34Slling 	(void) printf("\n");
1075b1b8ab34Slling }
1076ecd6cf80Smarks 
1077990b4856Slling /*
1078990b4856Slling  * Given a numeric suffix, convert the value into a number of bits that the
1079990b4856Slling  * resulting value must be shifted.
1080990b4856Slling  */
1081990b4856Slling static int
1082990b4856Slling str2shift(libzfs_handle_t *hdl, const char *buf)
1083990b4856Slling {
1084990b4856Slling 	const char *ends = "BKMGTPEZ";
1085990b4856Slling 	int i;
1086990b4856Slling 
1087990b4856Slling 	if (buf[0] == '\0')
1088990b4856Slling 		return (0);
1089990b4856Slling 	for (i = 0; i < strlen(ends); i++) {
1090990b4856Slling 		if (toupper(buf[0]) == ends[i])
1091990b4856Slling 			break;
1092990b4856Slling 	}
1093990b4856Slling 	if (i == strlen(ends)) {
1094990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1095990b4856Slling 		    "invalid numeric suffix '%s'"), buf);
1096990b4856Slling 		return (-1);
1097990b4856Slling 	}
1098990b4856Slling 
1099990b4856Slling 	/*
1100990b4856Slling 	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
1101990b4856Slling 	 * allow 'BB' - that's just weird.
1102990b4856Slling 	 */
1103990b4856Slling 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
1104990b4856Slling 	    toupper(buf[0]) != 'B'))
1105990b4856Slling 		return (10*i);
1106990b4856Slling 
1107990b4856Slling 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1108990b4856Slling 	    "invalid numeric suffix '%s'"), buf);
1109990b4856Slling 	return (-1);
1110990b4856Slling }
1111990b4856Slling 
1112990b4856Slling /*
1113990b4856Slling  * Convert a string of the form '100G' into a real number.  Used when setting
1114990b4856Slling  * properties or creating a volume.  'buf' is used to place an extended error
1115990b4856Slling  * message for the caller to use.
1116990b4856Slling  */
1117ecd6cf80Smarks int
1118990b4856Slling zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
1119ecd6cf80Smarks {
1120990b4856Slling 	char *end;
1121990b4856Slling 	int shift;
1122ecd6cf80Smarks 
1123990b4856Slling 	*num = 0;
1124990b4856Slling 
1125990b4856Slling 	/* Check to see if this looks like a number.  */
1126990b4856Slling 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
1127990b4856Slling 		if (hdl)
1128990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129990b4856Slling 			    "bad numeric value '%s'"), value);
1130990b4856Slling 		return (-1);
1131ecd6cf80Smarks 	}
1132ecd6cf80Smarks 
1133069f55e2SEric Schrock 	/* Rely on strtoull() to process the numeric portion.  */
1134990b4856Slling 	errno = 0;
1135f67f35c3SEric Schrock 	*num = strtoull(value, &end, 10);
1136990b4856Slling 
1137990b4856Slling 	/*
1138990b4856Slling 	 * Check for ERANGE, which indicates that the value is too large to fit
1139990b4856Slling 	 * in a 64-bit value.
1140990b4856Slling 	 */
1141990b4856Slling 	if (errno == ERANGE) {
1142990b4856Slling 		if (hdl)
1143990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1144990b4856Slling 			    "numeric value is too large"));
1145990b4856Slling 		return (-1);
1146990b4856Slling 	}
1147990b4856Slling 
1148990b4856Slling 	/*
1149990b4856Slling 	 * If we have a decimal value, then do the computation with floating
1150990b4856Slling 	 * point arithmetic.  Otherwise, use standard arithmetic.
1151990b4856Slling 	 */
1152990b4856Slling 	if (*end == '.') {
1153990b4856Slling 		double fval = strtod(value, &end);
1154990b4856Slling 
1155990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1156990b4856Slling 			return (-1);
1157990b4856Slling 
1158990b4856Slling 		fval *= pow(2, shift);
1159990b4856Slling 
1160990b4856Slling 		if (fval > UINT64_MAX) {
1161990b4856Slling 			if (hdl)
1162990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1163990b4856Slling 				    "numeric value is too large"));
1164990b4856Slling 			return (-1);
1165990b4856Slling 		}
1166990b4856Slling 
1167990b4856Slling 		*num = (uint64_t)fval;
1168990b4856Slling 	} else {
1169990b4856Slling 		if ((shift = str2shift(hdl, end)) == -1)
1170990b4856Slling 			return (-1);
1171990b4856Slling 
1172990b4856Slling 		/* Check for overflow */
1173990b4856Slling 		if (shift >= 64 || (*num << shift) >> shift != *num) {
1174990b4856Slling 			if (hdl)
1175990b4856Slling 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1176990b4856Slling 				    "numeric value is too large"));
1177990b4856Slling 			return (-1);
1178990b4856Slling 		}
1179990b4856Slling 
1180990b4856Slling 		*num <<= shift;
1181990b4856Slling 	}
1182990b4856Slling 
1183990b4856Slling 	return (0);
1184990b4856Slling }
1185990b4856Slling 
1186990b4856Slling /*
1187990b4856Slling  * Given a propname=value nvpair to set, parse any numeric properties
1188990b4856Slling  * (index, boolean, etc) if they are specified as strings and add the
1189990b4856Slling  * resulting nvpair to the returned nvlist.
1190990b4856Slling  *
1191990b4856Slling  * At the DSL layer, all properties are either 64-bit numbers or strings.
1192990b4856Slling  * We want the user to be able to ignore this fact and specify properties
1193990b4856Slling  * as native values (numbers, for example) or as strings (to simplify
1194990b4856Slling  * command line utilities).  This also handles converting index types
1195990b4856Slling  * (compression, checksum, etc) from strings to their on-disk index.
1196990b4856Slling  */
1197990b4856Slling int
1198990b4856Slling zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop,
1199990b4856Slling     zfs_type_t type, nvlist_t *ret, char **svalp, uint64_t *ivalp,
1200990b4856Slling     const char *errbuf)
1201990b4856Slling {
1202990b4856Slling 	data_type_t datatype = nvpair_type(elem);
1203990b4856Slling 	zprop_type_t proptype;
1204990b4856Slling 	const char *propname;
1205990b4856Slling 	char *value;
1206990b4856Slling 	boolean_t isnone = B_FALSE;
12071c10ae76SMike Gerdts 	boolean_t isauto = B_FALSE;
1208990b4856Slling 
1209990b4856Slling 	if (type == ZFS_TYPE_POOL) {
1210990b4856Slling 		proptype = zpool_prop_get_type(prop);
1211990b4856Slling 		propname = zpool_prop_to_name(prop);
1212990b4856Slling 	} else {
1213990b4856Slling 		proptype = zfs_prop_get_type(prop);
1214990b4856Slling 		propname = zfs_prop_to_name(prop);
1215990b4856Slling 	}
1216990b4856Slling 
1217990b4856Slling 	/*
1218990b4856Slling 	 * Convert any properties to the internal DSL value types.
1219990b4856Slling 	 */
1220990b4856Slling 	*svalp = NULL;
1221990b4856Slling 	*ivalp = 0;
1222990b4856Slling 
1223990b4856Slling 	switch (proptype) {
1224990b4856Slling 	case PROP_TYPE_STRING:
1225990b4856Slling 		if (datatype != DATA_TYPE_STRING) {
1226990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1227990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1228990b4856Slling 			goto error;
1229990b4856Slling 		}
1230990b4856Slling 		(void) nvpair_value_string(elem, svalp);
1231990b4856Slling 		if (strlen(*svalp) >= ZFS_MAXPROPLEN) {
1232990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1233990b4856Slling 			    "'%s' is too long"), nvpair_name(elem));
1234990b4856Slling 			goto error;
1235990b4856Slling 		}
1236990b4856Slling 		break;
1237990b4856Slling 
1238990b4856Slling 	case PROP_TYPE_NUMBER:
1239990b4856Slling 		if (datatype == DATA_TYPE_STRING) {
1240990b4856Slling 			(void) nvpair_value_string(elem, &value);
1241990b4856Slling 			if (strcmp(value, "none") == 0) {
1242990b4856Slling 				isnone = B_TRUE;
12431c10ae76SMike Gerdts 			} else if (strcmp(value, "auto") == 0) {
12441c10ae76SMike Gerdts 				isauto = B_TRUE;
12451c10ae76SMike Gerdts 			} else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) {
1246990b4856Slling 				goto error;
1247990b4856Slling 			}
1248990b4856Slling 		} else if (datatype == DATA_TYPE_UINT64) {
1249990b4856Slling 			(void) nvpair_value_uint64(elem, ivalp);
1250990b4856Slling 		} else {
1251990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1252990b4856Slling 			    "'%s' must be a number"), nvpair_name(elem));
1253990b4856Slling 			goto error;
1254990b4856Slling 		}
1255990b4856Slling 
1256990b4856Slling 		/*
1257990b4856Slling 		 * Quota special: force 'none' and don't allow 0.
1258990b4856Slling 		 */
1259a9799022Sck 		if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone &&
1260a9799022Sck 		    (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) {
1261990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262a9799022Sck 			    "use 'none' to disable quota/refquota"));
1263990b4856Slling 			goto error;
1264990b4856Slling 		}
1265a2afb611SJerry Jelinek 
1266a2afb611SJerry Jelinek 		/*
1267a2afb611SJerry Jelinek 		 * Special handling for "*_limit=none". In this case it's not
1268a2afb611SJerry Jelinek 		 * 0 but UINT64_MAX.
1269a2afb611SJerry Jelinek 		 */
1270a2afb611SJerry Jelinek 		if ((type & ZFS_TYPE_DATASET) && isnone &&
1271a2afb611SJerry Jelinek 		    (prop == ZFS_PROP_FILESYSTEM_LIMIT ||
1272a2afb611SJerry Jelinek 		    prop == ZFS_PROP_SNAPSHOT_LIMIT)) {
1273a2afb611SJerry Jelinek 			*ivalp = UINT64_MAX;
1274a2afb611SJerry Jelinek 		}
12751c10ae76SMike Gerdts 
12761c10ae76SMike Gerdts 		/*
12771c10ae76SMike Gerdts 		 * Special handling for setting 'refreservation' to 'auto'.  Use
12781c10ae76SMike Gerdts 		 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv().
12791c10ae76SMike Gerdts 		 * 'auto' is only allowed on volumes.
12801c10ae76SMike Gerdts 		 */
12811c10ae76SMike Gerdts 		if (isauto) {
12821c10ae76SMike Gerdts 			switch (prop) {
12831c10ae76SMike Gerdts 			case ZFS_PROP_REFRESERVATION:
12841c10ae76SMike Gerdts 				if ((type & ZFS_TYPE_VOLUME) == 0) {
12851c10ae76SMike Gerdts 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12861c10ae76SMike Gerdts 					    "'%s=auto' only allowed on "
12871c10ae76SMike Gerdts 					    "volumes"), nvpair_name(elem));
12881c10ae76SMike Gerdts 					goto error;
12891c10ae76SMike Gerdts 				}
12901c10ae76SMike Gerdts 				*ivalp = UINT64_MAX;
12911c10ae76SMike Gerdts 				break;
12921c10ae76SMike Gerdts 			default:
12931c10ae76SMike Gerdts 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
12941c10ae76SMike Gerdts 				    "'auto' is invalid value for '%s'"),
12951c10ae76SMike Gerdts 				    nvpair_name(elem));
12961c10ae76SMike Gerdts 				goto error;
12971c10ae76SMike Gerdts 			}
12981c10ae76SMike Gerdts 		}
12991c10ae76SMike Gerdts 
1300990b4856Slling 		break;
1301990b4856Slling 
1302990b4856Slling 	case PROP_TYPE_INDEX:
1303a9799022Sck 		if (datatype != DATA_TYPE_STRING) {
1304990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1305990b4856Slling 			    "'%s' must be a string"), nvpair_name(elem));
1306990b4856Slling 			goto error;
1307990b4856Slling 		}
1308990b4856Slling 
1309a9799022Sck 		(void) nvpair_value_string(elem, &value);
1310a9799022Sck 
1311990b4856Slling 		if (zprop_string_to_index(prop, value, ivalp, type) != 0) {
1312990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1313990b4856Slling 			    "'%s' must be one of '%s'"), propname,
1314990b4856Slling 			    zprop_values(prop, type));
1315990b4856Slling 			goto error;
1316990b4856Slling 		}
1317990b4856Slling 		break;
1318990b4856Slling 
1319990b4856Slling 	default:
1320990b4856Slling 		abort();
1321990b4856Slling 	}
1322990b4856Slling 
1323990b4856Slling 	/*
1324990b4856Slling 	 * Add the result to our return set of properties.
1325990b4856Slling 	 */
1326990b4856Slling 	if (*svalp != NULL) {
1327990b4856Slling 		if (nvlist_add_string(ret, propname, *svalp) != 0) {
1328990b4856Slling 			(void) no_memory(hdl);
1329990b4856Slling 			return (-1);
1330990b4856Slling 		}
1331990b4856Slling 	} else {
1332990b4856Slling 		if (nvlist_add_uint64(ret, propname, *ivalp) != 0) {
1333990b4856Slling 			(void) no_memory(hdl);
1334990b4856Slling 			return (-1);
1335990b4856Slling 		}
1336990b4856Slling 	}
1337990b4856Slling 
1338990b4856Slling 	return (0);
1339990b4856Slling error:
1340990b4856Slling 	(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1341990b4856Slling 	return (-1);
1342990b4856Slling }
1343990b4856Slling 
134474e7dc98SMatthew Ahrens static int
134574e7dc98SMatthew Ahrens addlist(libzfs_handle_t *hdl, char *propname, zprop_list_t **listp,
134674e7dc98SMatthew Ahrens     zfs_type_t type)
134774e7dc98SMatthew Ahrens {
134874e7dc98SMatthew Ahrens 	int prop;
134974e7dc98SMatthew Ahrens 	zprop_list_t *entry;
135074e7dc98SMatthew Ahrens 
135174e7dc98SMatthew Ahrens 	prop = zprop_name_to_prop(propname, type);
135274e7dc98SMatthew Ahrens 
135374e7dc98SMatthew Ahrens 	if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type))
135474e7dc98SMatthew Ahrens 		prop = ZPROP_INVAL;
135574e7dc98SMatthew Ahrens 
135674e7dc98SMatthew Ahrens 	/*
135774e7dc98SMatthew Ahrens 	 * When no property table entry can be found, return failure if
135874e7dc98SMatthew Ahrens 	 * this is a pool property or if this isn't a user-defined
135974e7dc98SMatthew Ahrens 	 * dataset property,
136074e7dc98SMatthew Ahrens 	 */
1361ad135b5dSChristopher Siden 	if (prop == ZPROP_INVAL && ((type == ZFS_TYPE_POOL &&
1362ad135b5dSChristopher Siden 	    !zpool_prop_feature(propname) &&
1363ad135b5dSChristopher Siden 	    !zpool_prop_unsupported(propname)) ||
1364ad135b5dSChristopher Siden 	    (type == ZFS_TYPE_DATASET && !zfs_prop_user(propname) &&
1365ad135b5dSChristopher Siden 	    !zfs_prop_userquota(propname) && !zfs_prop_written(propname)))) {
136674e7dc98SMatthew Ahrens 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136774e7dc98SMatthew Ahrens 		    "invalid property '%s'"), propname);
136874e7dc98SMatthew Ahrens 		return (zfs_error(hdl, EZFS_BADPROP,
136974e7dc98SMatthew Ahrens 		    dgettext(TEXT_DOMAIN, "bad property list")));
137074e7dc98SMatthew Ahrens 	}
137174e7dc98SMatthew Ahrens 
137274e7dc98SMatthew Ahrens 	if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
137374e7dc98SMatthew Ahrens 		return (-1);
137474e7dc98SMatthew Ahrens 
137574e7dc98SMatthew Ahrens 	entry->pl_prop = prop;
137674e7dc98SMatthew Ahrens 	if (prop == ZPROP_INVAL) {
1377ad135b5dSChristopher Siden 		if ((entry->pl_user_prop = zfs_strdup(hdl, propname)) ==
1378ad135b5dSChristopher Siden 		    NULL) {
137974e7dc98SMatthew Ahrens 			free(entry);
138074e7dc98SMatthew Ahrens 			return (-1);
138174e7dc98SMatthew Ahrens 		}
138274e7dc98SMatthew Ahrens 		entry->pl_width = strlen(propname);
138374e7dc98SMatthew Ahrens 	} else {
138474e7dc98SMatthew Ahrens 		entry->pl_width = zprop_width(prop, &entry->pl_fixed,
138574e7dc98SMatthew Ahrens 		    type);
138674e7dc98SMatthew Ahrens 	}
138774e7dc98SMatthew Ahrens 
138874e7dc98SMatthew Ahrens 	*listp = entry;
138974e7dc98SMatthew Ahrens 
139074e7dc98SMatthew Ahrens 	return (0);
139174e7dc98SMatthew Ahrens }
139274e7dc98SMatthew Ahrens 
1393990b4856Slling /*
1394990b4856Slling  * Given a comma-separated list of properties, construct a property list
1395990b4856Slling  * containing both user-defined and native properties.  This function will
1396990b4856Slling  * return a NULL list if 'all' is specified, which can later be expanded
1397990b4856Slling  * by zprop_expand_list().
1398990b4856Slling  */
1399990b4856Slling int
1400990b4856Slling zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp,
1401990b4856Slling     zfs_type_t type)
1402990b4856Slling {
1403990b4856Slling 	*listp = NULL;
1404990b4856Slling 
1405990b4856Slling 	/*
1406990b4856Slling 	 * If 'all' is specified, return a NULL list.
1407990b4856Slling 	 */
1408990b4856Slling 	if (strcmp(props, "all") == 0)
1409990b4856Slling 		return (0);
1410990b4856Slling 
1411990b4856Slling 	/*
1412990b4856Slling 	 * If no props were specified, return an error.
1413990b4856Slling 	 */
1414990b4856Slling 	if (props[0] == '\0') {
1415990b4856Slling 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1416990b4856Slling 		    "no properties specified"));
1417990b4856Slling 		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
1418990b4856Slling 		    "bad property list")));
1419990b4856Slling 	}
1420990b4856Slling 
1421990b4856Slling 	/*
1422990b4856Slling 	 * It would be nice to use getsubopt() here, but the inclusion of column
1423990b4856Slling 	 * aliases makes this more effort than it's worth.
1424990b4856Slling 	 */
142574e7dc98SMatthew Ahrens 	while (*props != '\0') {
142674e7dc98SMatthew Ahrens 		size_t len;
142774e7dc98SMatthew Ahrens 		char *p;
142874e7dc98SMatthew Ahrens 		char c;
142974e7dc98SMatthew Ahrens 
143074e7dc98SMatthew Ahrens 		if ((p = strchr(props, ',')) == NULL) {
143174e7dc98SMatthew Ahrens 			len = strlen(props);
143274e7dc98SMatthew Ahrens 			p = props + len;
1433990b4856Slling 		} else {
143474e7dc98SMatthew Ahrens 			len = p - props;
1435990b4856Slling 		}
1436990b4856Slling 
1437990b4856Slling 		/*
1438990b4856Slling 		 * Check for empty options.
1439990b4856Slling 		 */
1440990b4856Slling 		if (len == 0) {
1441990b4856Slling 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442990b4856Slling 			    "empty property name"));
1443990b4856Slling 			return (zfs_error(hdl, EZFS_BADPROP,
1444990b4856Slling 			    dgettext(TEXT_DOMAIN, "bad property list")));
1445990b4856Slling 		}
1446990b4856Slling 
1447990b4856Slling 		/*
1448990b4856Slling 		 * Check all regular property names.
1449990b4856Slling 		 */
145074e7dc98SMatthew Ahrens 		c = props[len];
145174e7dc98SMatthew Ahrens 		props[len] = '\0';
145274e7dc98SMatthew Ahrens 
145374e7dc98SMatthew Ahrens 		if (strcmp(props, "space") == 0) {
145474e7dc98SMatthew Ahrens 			static char *spaceprops[] = {
145574e7dc98SMatthew Ahrens 				"name", "avail", "used", "usedbysnapshots",
145674e7dc98SMatthew Ahrens 				"usedbydataset", "usedbyrefreservation",
145774e7dc98SMatthew Ahrens 				"usedbychildren", NULL
145874e7dc98SMatthew Ahrens 			};
145974e7dc98SMatthew Ahrens 			int i;
146074e7dc98SMatthew Ahrens 
146174e7dc98SMatthew Ahrens 			for (i = 0; spaceprops[i]; i++) {
146274e7dc98SMatthew Ahrens 				if (addlist(hdl, spaceprops[i], listp, type))
146374e7dc98SMatthew Ahrens 					return (-1);
146474e7dc98SMatthew Ahrens 				listp = &(*listp)->pl_next;
1465990b4856Slling 			}
1466990b4856Slling 		} else {
146774e7dc98SMatthew Ahrens 			if (addlist(hdl, props, listp, type))
146874e7dc98SMatthew Ahrens 				return (-1);
146974e7dc98SMatthew Ahrens 			listp = &(*listp)->pl_next;
1470990b4856Slling 		}
1471990b4856Slling 
147274e7dc98SMatthew Ahrens 		props = p;
1473990b4856Slling 		if (c == ',')
147474e7dc98SMatthew Ahrens 			props++;
1475990b4856Slling 	}
1476990b4856Slling 
1477990b4856Slling 	return (0);
1478990b4856Slling }
1479990b4856Slling 
1480990b4856Slling void
1481990b4856Slling zprop_free_list(zprop_list_t *pl)
1482990b4856Slling {
1483990b4856Slling 	zprop_list_t *next;
1484990b4856Slling 
1485990b4856Slling 	while (pl != NULL) {
1486990b4856Slling 		next = pl->pl_next;
1487990b4856Slling 		free(pl->pl_user_prop);
1488990b4856Slling 		free(pl);
1489990b4856Slling 		pl = next;
1490990b4856Slling 	}
1491990b4856Slling }
1492990b4856Slling 
1493990b4856Slling typedef struct expand_data {
1494990b4856Slling 	zprop_list_t	**last;
1495990b4856Slling 	libzfs_handle_t	*hdl;
1496990b4856Slling 	zfs_type_t type;
1497990b4856Slling } expand_data_t;
1498990b4856Slling 
1499990b4856Slling int
1500990b4856Slling zprop_expand_list_cb(int prop, void *cb)
1501990b4856Slling {
1502990b4856Slling 	zprop_list_t *entry;
1503990b4856Slling 	expand_data_t *edp = cb;
1504990b4856Slling 
1505990b4856Slling 	if ((entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t))) == NULL)
1506990b4856Slling 		return (ZPROP_INVAL);
1507990b4856Slling 
1508990b4856Slling 	entry->pl_prop = prop;
1509990b4856Slling 	entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type);
1510990b4856Slling 	entry->pl_all = B_TRUE;
1511990b4856Slling 
1512990b4856Slling 	*(edp->last) = entry;
1513990b4856Slling 	edp->last = &entry->pl_next;
1514990b4856Slling 
1515990b4856Slling 	return (ZPROP_CONT);
1516990b4856Slling }
1517990b4856Slling 
1518990b4856Slling int
1519990b4856Slling zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type)
1520990b4856Slling {
1521990b4856Slling 	zprop_list_t *entry;
1522990b4856Slling 	zprop_list_t **last;
1523990b4856Slling 	expand_data_t exp;
1524990b4856Slling 
1525990b4856Slling 	if (*plp == NULL) {
1526990b4856Slling 		/*
1527990b4856Slling 		 * If this is the very first time we've been called for an 'all'
1528990b4856Slling 		 * specification, expand the list to include all native
1529990b4856Slling 		 * properties.
1530990b4856Slling 		 */
1531990b4856Slling 		last = plp;
1532990b4856Slling 
1533990b4856Slling 		exp.last = last;
1534990b4856Slling 		exp.hdl = hdl;
1535990b4856Slling 		exp.type = type;
1536990b4856Slling 
1537990b4856Slling 		if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE,
1538990b4856Slling 		    B_FALSE, type) == ZPROP_INVAL)
1539990b4856Slling 			return (-1);
1540990b4856Slling 
1541990b4856Slling 		/*
1542990b4856Slling 		 * Add 'name' to the beginning of the list, which is handled
1543990b4856Slling 		 * specially.
1544990b4856Slling 		 */
1545990b4856Slling 		if ((entry = zfs_alloc(hdl, sizeof (zprop_list_t))) == NULL)
1546990b4856Slling 			return (-1);
1547990b4856Slling 
1548990b4856Slling 		entry->pl_prop = (type == ZFS_TYPE_POOL) ?  ZPOOL_PROP_NAME :
1549990b4856Slling 		    ZFS_PROP_NAME;
1550990b4856Slling 		entry->pl_width = zprop_width(entry->pl_prop,
1551990b4856Slling 		    &entry->pl_fixed, type);
1552990b4856Slling 		entry->pl_all = B_TRUE;
1553990b4856Slling 		entry->pl_next = *plp;
1554990b4856Slling 		*plp = entry;
1555990b4856Slling 	}
1556990b4856Slling 	return (0);
1557990b4856Slling }
1558990b4856Slling 
1559990b4856Slling int
1560990b4856Slling zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered,
1561990b4856Slling     zfs_type_t type)
1562990b4856Slling {
1563990b4856Slling 	return (zprop_iter_common(func, cb, show_all, ordered, type));
1564ecd6cf80Smarks }
15652bcf0248SMax Grossman 
15662bcf0248SMax Grossman /*
15672bcf0248SMax Grossman  * zfs_get_hole_count retrieves the number of holes (blocks which are
15682bcf0248SMax Grossman  * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It
15692bcf0248SMax Grossman  * also optionally fetches the block size when bs is non-NULL. With hole count
15702bcf0248SMax Grossman  * and block size the full space consumed by the holes of a file can be
15712bcf0248SMax Grossman  * calculated.
15722bcf0248SMax Grossman  *
15732bcf0248SMax Grossman  * On success, zero is returned, the count argument is set to the
15742bcf0248SMax Grossman  * number of holes, and the bs argument is set to the block size (if it is
15752bcf0248SMax Grossman  * not NULL). On error, a non-zero errno is returned and the values in count
15762bcf0248SMax Grossman  * and bs are undefined.
15772bcf0248SMax Grossman  */
15782bcf0248SMax Grossman int
15799a686fbcSPaul Dagnelie zfs_get_hole_count(const char *path, uint64_t *count, uint64_t *bs)
15809a686fbcSPaul Dagnelie {
15812bcf0248SMax Grossman 	int fd, err;
15822bcf0248SMax Grossman 	struct stat64 ss;
15832bcf0248SMax Grossman 	uint64_t fill;
15842bcf0248SMax Grossman 
15852bcf0248SMax Grossman 	fd = open(path, O_RDONLY | O_LARGEFILE);
15862bcf0248SMax Grossman 	if (fd == -1)
15872bcf0248SMax Grossman 		return (errno);
15882bcf0248SMax Grossman 
15892bcf0248SMax Grossman 	if (ioctl(fd, _FIO_COUNT_FILLED, &fill) == -1) {
15902bcf0248SMax Grossman 		err = errno;
15912bcf0248SMax Grossman 		(void) close(fd);
15922bcf0248SMax Grossman 		return (err);
15932bcf0248SMax Grossman 	}
15942bcf0248SMax Grossman 
15952bcf0248SMax Grossman 	if (fstat64(fd, &ss) == -1) {
15962bcf0248SMax Grossman 		err = errno;
15972bcf0248SMax Grossman 		(void) close(fd);
15982bcf0248SMax Grossman 		return (err);
15992bcf0248SMax Grossman 	}
16002bcf0248SMax Grossman 
16012bcf0248SMax Grossman 	*count = (ss.st_size + ss.st_blksize - 1) / ss.st_blksize - fill;
16022bcf0248SMax Grossman 	VERIFY3S(*count, >=, 0);
16032bcf0248SMax Grossman 	if (bs != NULL) {
16042bcf0248SMax Grossman 		*bs = ss.st_blksize;
16052bcf0248SMax Grossman 	}
16062bcf0248SMax Grossman 
16072bcf0248SMax Grossman 	if (close(fd) == -1) {
16082bcf0248SMax Grossman 		return (errno);
16092bcf0248SMax Grossman 	}
16102bcf0248SMax Grossman 	return (0);
16112bcf0248SMax Grossman }
1612e0f1c0afSOlaf Faaland 
1613e0f1c0afSOlaf Faaland ulong_t
1614e0f1c0afSOlaf Faaland get_system_hostid(void)
1615e0f1c0afSOlaf Faaland {
1616e0f1c0afSOlaf Faaland 	char *env;
1617e0f1c0afSOlaf Faaland 
1618e0f1c0afSOlaf Faaland 	/*
1619e0f1c0afSOlaf Faaland 	 * Allow the hostid to be subverted for testing.
1620e0f1c0afSOlaf Faaland 	 */
1621e0f1c0afSOlaf Faaland 	env = getenv("ZFS_HOSTID");
1622e0f1c0afSOlaf Faaland 	if (env) {
1623e0f1c0afSOlaf Faaland 		ulong_t hostid = strtoull(env, NULL, 16);
1624e0f1c0afSOlaf Faaland 		return (hostid & 0xFFFFFFFF);
1625e0f1c0afSOlaf Faaland 	}
1626e0f1c0afSOlaf Faaland 
1627e0f1c0afSOlaf Faaland 	return (gethostid());
1628e0f1c0afSOlaf Faaland }
1629