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.
25*7ac89354SDon 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"
56d8ab6e12SDon Brady #include <libzutil.h>
57fa9e4066Sahrens 
5899653d4eSeschrock int
libzfs_errno(libzfs_handle_t * hdl)5999653d4eSeschrock libzfs_errno(libzfs_handle_t *hdl)
6099653d4eSeschrock {
6199653d4eSeschrock 	return (hdl->libzfs_error);
6299653d4eSeschrock }
63fa9e4066Sahrens 
6499653d4eSeschrock const char *
libzfs_error_action(libzfs_handle_t * hdl)6599653d4eSeschrock libzfs_error_action(libzfs_handle_t *hdl)
6699653d4eSeschrock {
6799653d4eSeschrock 	return (hdl->libzfs_action);
6899653d4eSeschrock }
69fa9e4066Sahrens 
7099653d4eSeschrock const char *
libzfs_error_description(libzfs_handle_t * hdl)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"));
205*7ac89354SDon Brady 	case EZFS_IOC_NOTSUPPORTED:
206*7ac89354SDon Brady 		return (dgettext(TEXT_DOMAIN, "operation not supported by "
207*7ac89354SDon Brady 		    "zfs kernel module"));
20889a89ebfSlling 	case EZFS_ACTIVE_SPARE:
20989a89ebfSlling 		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
21089a89ebfSlling 		    "device"));
211e6ca193dSGeorge Wilson 	case EZFS_UNPLAYED_LOGS:
212e6ca193dSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
213e6ca193dSGeorge Wilson 		    "logs"));
214842727c2SChris Kirby 	case EZFS_REFTAG_RELE:
215842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
216842727c2SChris Kirby 	case EZFS_REFTAG_HOLD:
217842727c2SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
218842727c2SChris Kirby 		    "dataset"));
219ca45db41SChris Kirby 	case EZFS_TAGTOOLONG:
220ca45db41SChris Kirby 		return (dgettext(TEXT_DOMAIN, "tag too long"));
2219e69d7d0SLori Alt 	case EZFS_PIPEFAILED:
2229e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
2239e69d7d0SLori Alt 	case EZFS_THREADCREATEFAILED:
2249e69d7d0SLori Alt 		return (dgettext(TEXT_DOMAIN, "thread create failed"));
2251195e687SMark J Musante 	case EZFS_POSTSPLIT_ONLINE:
2261195e687SMark J Musante 		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
2271195e687SMark J Musante 		    "into a new one"));
2281702cce7SAlek Pinchuk 	case EZFS_SCRUB_PAUSED:
2291702cce7SAlek Pinchuk 		return (dgettext(TEXT_DOMAIN, "scrub is paused; "
2301702cce7SAlek Pinchuk 		    "use 'zpool scrub' to resume"));
2313f9d6ad7SLin Ling 	case EZFS_SCRUBBING:
2323f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
2333f9d6ad7SLin Ling 		    "use 'zpool scrub -s' to cancel current scrub"));
2343f9d6ad7SLin Ling 	case EZFS_NO_SCRUB:
2353f9d6ad7SLin Ling 		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
23699d5e173STim Haley 	case EZFS_DIFF:
23799d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
23899d5e173STim Haley 	case EZFS_DIFFDATA:
23999d5e173STim Haley 		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
240f9af39baSGeorge Wilson 	case EZFS_POOLREADONLY:
241f9af39baSGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
2425cabbc6bSPrashanth Sreenivasa 	case EZFS_NO_PENDING:
2435cabbc6bSPrashanth Sreenivasa 		return (dgettext(TEXT_DOMAIN, "operation is not "
2445cabbc6bSPrashanth Sreenivasa 		    "in progress"));
24586714001SSerapheim Dimitropoulos 	case EZFS_CHECKPOINT_EXISTS:
24686714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
24786714001SSerapheim Dimitropoulos 	case EZFS_DISCARDING_CHECKPOINT:
24886714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "currently discarding "
24986714001SSerapheim Dimitropoulos 		    "checkpoint"));
25086714001SSerapheim Dimitropoulos 	case EZFS_NO_CHECKPOINT:
25186714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
25286714001SSerapheim Dimitropoulos 	case EZFS_DEVRM_IN_PROGRESS:
25386714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "device removal in progress"));
25486714001SSerapheim Dimitropoulos 	case EZFS_VDEV_TOO_BIG:
25586714001SSerapheim Dimitropoulos 		return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
256e0f1c0afSOlaf Faaland 	case EZFS_ACTIVE_POOL:
257e0f1c0afSOlaf Faaland 		return (dgettext(TEXT_DOMAIN, "pool is imported on a "
258e0f1c0afSOlaf Faaland 		    "different host"));
259084fd14fSBrian Behlendorf 	case EZFS_CRYPTOFAILED:
260084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "encryption failure"));
261094e47e9SGeorge Wilson 	case EZFS_TOOMANY:
262094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "argument list too long"));
263094e47e9SGeorge Wilson 	case EZFS_INITIALIZING:
264094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "currently initializing"));
265094e47e9SGeorge Wilson 	case EZFS_NO_INITIALIZE:
266094e47e9SGeorge Wilson 		return (dgettext(TEXT_DOMAIN, "there is no active "
267094e47e9SGeorge Wilson 		    "initialization"));
2686ccda740Sloli 	case EZFS_WRONG_PARENT:
2696ccda740Sloli 		return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
270084fd14fSBrian Behlendorf 	case EZFS_TRIMMING:
271084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "currently trimming"));
272084fd14fSBrian Behlendorf 	case EZFS_NO_TRIM:
273084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "there is no active trim"));
274084fd14fSBrian Behlendorf 	case EZFS_TRIM_NOTSUP:
275084fd14fSBrian Behlendorf 		return (dgettext(TEXT_DOMAIN, "trim operations are not "
276084fd14fSBrian Behlendorf 		    "supported by this device"));
277e4c795beSTom Caputi 	case EZFS_NO_RESILVER_DEFER:
278e4c795beSTom Caputi 		return (dgettext(TEXT_DOMAIN, "this action requires the "
279e4c795beSTom Caputi 		    "resilver_defer feature"));
28099653d4eSeschrock 	case EZFS_UNKNOWN:
28199653d4eSeschrock 		return (dgettext(TEXT_DOMAIN, "unknown error"));
28299653d4eSeschrock 	default:
283c08432ebSeschrock 		assert(hdl->libzfs_error == 0);
284c08432ebSeschrock 		return (dgettext(TEXT_DOMAIN, "no error"));
28599653d4eSeschrock 	}
28699653d4eSeschrock }
28799653d4eSeschrock 
28899653d4eSeschrock /*PRINTFLIKE2*/
289fa9e4066Sahrens void
zfs_error_aux(libzfs_handle_t * hdl,const char * fmt,...)29099653d4eSeschrock zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
291fa9e4066Sahrens {
292fa9e4066Sahrens 	va_list ap;
293fa9e4066Sahrens 
294fa9e4066Sahrens 	va_start(ap, fmt);
295fa9e4066Sahrens 
29699653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
29799653d4eSeschrock 	    fmt, ap);
29899653d4eSeschrock 	hdl->libzfs_desc_active = 1;
29999653d4eSeschrock 
30099653d4eSeschrock 	va_end(ap);
30199653d4eSeschrock }
30299653d4eSeschrock 
30399653d4eSeschrock static void
zfs_verror(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)30499653d4eSeschrock zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
30599653d4eSeschrock {
30699653d4eSeschrock 	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
30799653d4eSeschrock 	    fmt, ap);
30899653d4eSeschrock 	hdl->libzfs_error = error;
30999653d4eSeschrock 
31099653d4eSeschrock 	if (hdl->libzfs_desc_active)
31199653d4eSeschrock 		hdl->libzfs_desc_active = 0;
31299653d4eSeschrock 	else
31399653d4eSeschrock 		hdl->libzfs_desc[0] = '\0';
31499653d4eSeschrock 
31599653d4eSeschrock 	if (hdl->libzfs_printerr) {
31699653d4eSeschrock 		if (error == EZFS_UNKNOWN) {
31799653d4eSeschrock 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
31899653d4eSeschrock 			    "error: %s\n"), libzfs_error_description(hdl));
31999653d4eSeschrock 			abort();
32099653d4eSeschrock 		}
32199653d4eSeschrock 
32299653d4eSeschrock 		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
323b1b8ab34Slling 		    libzfs_error_description(hdl));
32499653d4eSeschrock 		if (error == EZFS_NOMEM)
32599653d4eSeschrock 			exit(1);
326fa9e4066Sahrens 	}
32799653d4eSeschrock }
32899653d4eSeschrock 
329ece3d9b3Slling int
zfs_error(libzfs_handle_t * hdl,int error,const char * msg)330ece3d9b3Slling zfs_error(libzfs_handle_t *hdl, int error, const char *msg)
331ece3d9b3Slling {
332ece3d9b3Slling 	return (zfs_error_fmt(hdl, error, "%s", msg));
333ece3d9b3Slling }
334ece3d9b3Slling 
33599653d4eSeschrock /*PRINTFLIKE3*/
33699653d4eSeschrock int
zfs_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)337ece3d9b3Slling zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
33899653d4eSeschrock {
33999653d4eSeschrock 	va_list ap;
34099653d4eSeschrock 
34199653d4eSeschrock 	va_start(ap, fmt);
34299653d4eSeschrock 
34399653d4eSeschrock 	zfs_verror(hdl, error, fmt, ap);
344fa9e4066Sahrens 
345fa9e4066Sahrens 	va_end(ap);
34699653d4eSeschrock 
34799653d4eSeschrock 	return (-1);
348fa9e4066Sahrens }
349fa9e4066Sahrens 
35099653d4eSeschrock static int
zfs_common_error(libzfs_handle_t * hdl,int error,const char * fmt,va_list ap)35199653d4eSeschrock zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
35299653d4eSeschrock     va_list ap)
35399653d4eSeschrock {
35499653d4eSeschrock 	switch (error) {
35599653d4eSeschrock 	case EPERM:
35699653d4eSeschrock 	case EACCES:
35799653d4eSeschrock 		zfs_verror(hdl, EZFS_PERM, fmt, ap);
35899653d4eSeschrock 		return (-1);
35999653d4eSeschrock 
360ecd6cf80Smarks 	case ECANCELED:
361ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
362ecd6cf80Smarks 		return (-1);
363ecd6cf80Smarks 
36499653d4eSeschrock 	case EIO:
36599653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
36699653d4eSeschrock 		return (-1);
36799653d4eSeschrock 
3686e27f868SSam Falkner 	case EFAULT:
3696e27f868SSam Falkner 		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
3706e27f868SSam Falkner 		return (-1);
3716e27f868SSam Falkner 
37299653d4eSeschrock 	case EINTR:
37399653d4eSeschrock 		zfs_verror(hdl, EZFS_INTR, fmt, ap);
37499653d4eSeschrock 		return (-1);
37599653d4eSeschrock 	}
37699653d4eSeschrock 
37799653d4eSeschrock 	return (0);
37899653d4eSeschrock }
37999653d4eSeschrock 
380ece3d9b3Slling int
zfs_standard_error(libzfs_handle_t * hdl,int error,const char * msg)381ece3d9b3Slling zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
382ece3d9b3Slling {
383ece3d9b3Slling 	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
384ece3d9b3Slling }
385ece3d9b3Slling 
38699653d4eSeschrock /*PRINTFLIKE3*/
38799653d4eSeschrock int
zfs_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)388ece3d9b3Slling zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
389fa9e4066Sahrens {
390fa9e4066Sahrens 	va_list ap;
391fa9e4066Sahrens 
392fa9e4066Sahrens 	va_start(ap, fmt);
393fa9e4066Sahrens 
39499653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
39599653d4eSeschrock 		va_end(ap);
39699653d4eSeschrock 		return (-1);
397fa9e4066Sahrens 	}
398fa9e4066Sahrens 
39999653d4eSeschrock 	switch (error) {
40099653d4eSeschrock 	case ENXIO:
40197d9e3a6Sck 	case ENODEV:
40219b94df9SMatthew Ahrens 	case EPIPE:
40399653d4eSeschrock 		zfs_verror(hdl, EZFS_IO, fmt, ap);
40499653d4eSeschrock 		break;
40599653d4eSeschrock 
40699653d4eSeschrock 	case ENOENT:
40799653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40899653d4eSeschrock 		    "dataset does not exist"));
40999653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
41099653d4eSeschrock 		break;
41199653d4eSeschrock 
41299653d4eSeschrock 	case ENOSPC:
41399653d4eSeschrock 	case EDQUOT:
41499653d4eSeschrock 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
41599653d4eSeschrock 		return (-1);
41699653d4eSeschrock 
41799653d4eSeschrock 	case EEXIST:
41899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41999653d4eSeschrock 		    "dataset already exists"));
42099653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
42199653d4eSeschrock 		break;
42299653d4eSeschrock 
42399653d4eSeschrock 	case EBUSY:
42499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42599653d4eSeschrock 		    "dataset is busy"));
42699653d4eSeschrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
42799653d4eSeschrock 		break;
428ecd6cf80Smarks 	case EROFS:
429f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
430ecd6cf80Smarks 		break;
431b7661cccSmmusante 	case ENAMETOOLONG:
432b7661cccSmmusante 		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
433b7661cccSmmusante 		break;
43440ff3960Sck 	case ENOTSUP:
43540ff3960Sck 		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
43640ff3960Sck 		break;
43754d692b7SGeorge Wilson 	case EAGAIN:
43854d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43954d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
44054d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
44154d692b7SGeorge Wilson 		break;
442e0f1c0afSOlaf Faaland 	case EREMOTEIO:
443e0f1c0afSOlaf Faaland 		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
444e0f1c0afSOlaf Faaland 		break;
445*7ac89354SDon Brady 	case ZFS_ERR_IOC_CMD_UNAVAIL:
446*7ac89354SDon Brady 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
447*7ac89354SDon Brady 		    "module does not support this operation. A reboot may "
448*7ac89354SDon Brady 		    "be required to enable this operation."));
449*7ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
450*7ac89354SDon Brady 		break;
451*7ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_UNAVAIL:
452*7ac89354SDon Brady 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
453*7ac89354SDon Brady 		    "module does not support an option for this operation. "
454*7ac89354SDon Brady 		    "A reboot may be required to enable this option."));
455*7ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
456*7ac89354SDon Brady 		break;
457*7ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_REQUIRED:
458*7ac89354SDon Brady 	case ZFS_ERR_IOC_ARG_BADTYPE:
459*7ac89354SDon Brady 		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
460*7ac89354SDon Brady 		break;
46199653d4eSeschrock 	default:
46292241e0bSTom Erickson 		zfs_error_aux(hdl, strerror(error));
46399653d4eSeschrock 		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
46499653d4eSeschrock 		break;
46599653d4eSeschrock 	}
46699653d4eSeschrock 
46799653d4eSeschrock 	va_end(ap);
46899653d4eSeschrock 	return (-1);
469fa9e4066Sahrens }
470fa9e4066Sahrens 
471ece3d9b3Slling int
zpool_standard_error(libzfs_handle_t * hdl,int error,const char * msg)472ece3d9b3Slling zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
473ece3d9b3Slling {
474ece3d9b3Slling 	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
475ece3d9b3Slling }
476ece3d9b3Slling 
47799653d4eSeschrock /*PRINTFLIKE3*/
47899653d4eSeschrock int
zpool_standard_error_fmt(libzfs_handle_t * hdl,int error,const char * fmt,...)479ece3d9b3Slling zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
480fa9e4066Sahrens {
48199653d4eSeschrock 	va_list ap;
48299653d4eSeschrock 
48399653d4eSeschrock 	va_start(ap, fmt);
48499653d4eSeschrock 
48599653d4eSeschrock 	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
48699653d4eSeschrock 		va_end(ap);
48799653d4eSeschrock 		return (-1);
48899653d4eSeschrock 	}
48999653d4eSeschrock 
49099653d4eSeschrock 	switch (error) {
49199653d4eSeschrock 	case ENODEV:
49299653d4eSeschrock 		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
49399653d4eSeschrock 		break;
49499653d4eSeschrock 
49599653d4eSeschrock 	case ENOENT:
496b1b8ab34Slling 		zfs_error_aux(hdl,
497b1b8ab34Slling 		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
49899653d4eSeschrock 		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
49999653d4eSeschrock 		break;
50099653d4eSeschrock 
50199653d4eSeschrock 	case EEXIST:
50299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50399653d4eSeschrock 		    "pool already exists"));
50499653d4eSeschrock 		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
50599653d4eSeschrock 		break;
50699653d4eSeschrock 
50799653d4eSeschrock 	case EBUSY:
50899653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
50943afaaa8SEric Schrock 		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
51099653d4eSeschrock 		break;
51199653d4eSeschrock 
51299653d4eSeschrock 	case ENXIO:
51399653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51499653d4eSeschrock 		    "one or more devices is currently unavailable"));
51599653d4eSeschrock 		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
51699653d4eSeschrock 		break;
51799653d4eSeschrock 
51899653d4eSeschrock 	case ENAMETOOLONG:
51999653d4eSeschrock 		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
52099653d4eSeschrock 		break;
52199653d4eSeschrock 
522b1b8ab34Slling 	case ENOTSUP:
523b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
524b1b8ab34Slling 		break;
525b1b8ab34Slling 
526b1b8ab34Slling 	case EINVAL:
527b1b8ab34Slling 		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
528b1b8ab34Slling 		break;
529b1b8ab34Slling 
530ecd6cf80Smarks 	case ENOSPC:
531ecd6cf80Smarks 	case EDQUOT:
532ecd6cf80Smarks 		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
533ecd6cf80Smarks 		return (-1);
534f9af39baSGeorge Wilson 
53554d692b7SGeorge Wilson 	case EAGAIN:
53654d692b7SGeorge Wilson 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
53754d692b7SGeorge Wilson 		    "pool I/O is currently suspended"));
53854d692b7SGeorge Wilson 		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
53954d692b7SGeorge Wilson 		break;
540ecd6cf80Smarks 
541f9af39baSGeorge Wilson 	case EROFS:
542f9af39baSGeorge Wilson 		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
543f9af39baSGeorge Wilson 		break;
5445cabbc6bSPrashanth Sreenivasa 	/* There is no pending operation to cancel */
545