1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21ad135b5Christopher Siden
22fa9e406ahrens/*
233f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24d8ab6e1Don Brady * Copyright 2020 Joyent, Inc.
257ac8935Don Brady * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
2688f61deIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
271702cceAlek Pinchuk * Copyright (c) 2017 Datto Inc.
28fa9e406ahrens */
29fa9e406ahrens
30fa9e406ahrens/*
31fa9e406ahrens * Internal utility routines for the ZFS library.
32fa9e406ahrens */
33fa9e406ahrens
34d8ab6e1Don Brady#include <ctype.h>
35fa9e406ahrens#include <errno.h>
36fa9e406ahrens#include <fcntl.h>
37fa9e406ahrens#include <libintl.h>
38fa9e406ahrens#include <stdarg.h>
39fa9e406ahrens#include <stdio.h>
40fa9e406ahrens#include <stdlib.h>
41fa9e406ahrens#include <strings.h>
42990b485lling#include <math.h>
432bcf024Max Grossman#include <sys/filio.h>
44fa9e406ahrens#include <sys/mnttab.h>
455aba80dck#include <sys/mntent.h>
465aba80dck#include <sys/types.h>
470a05512Jason King#include <libcmdutils.h>
48fa9e406ahrens
49fa9e406ahrens#include <libzfs.h>
504445fffMatthew Ahrens#include <libzfs_core.h>
51fa9e406ahrens
52fa9e406ahrens#include "libzfs_impl.h"
5391ebeefahrens#include "zfs_prop.h"
54e0f1c0aOlaf Faaland#include "zfs_comutil.h"
55ad135b5Christopher Siden#include "zfeature_common.h"
56d8ab6e1Don Brady#include <libzutil.h>
57fa9e406ahrens
5899653d4eschrockint
5999653d4eschrocklibzfs_errno(libzfs_handle_t *hdl)
6099653d4eschrock{
6199653d4eschrock	return (hdl->libzfs_error);
6299653d4eschrock}
63fa9e406ahrens
6499653d4eschrockconst char *
6599653d4eschrocklibzfs_error_action(libzfs_handle_t *hdl)
6699653d4eschrock{
6799653d4eschrock	return (hdl->libzfs_action);
6899653d4eschrock}
69fa9e406ahrens
7099653d4eschrockconst char *
7199653d4eschrocklibzfs_error_description(libzfs_handle_t *hdl)
7299653d4eschrock{
7399653d4eschrock	if (hdl->libzfs_desc[0] != '\0')
7499653d4eschrock		return (hdl->libzfs_desc);
7599653d4eschrock
7699653d4eschrock	switch (hdl->libzfs_error) {
7799653d4eschrock	case EZFS_NOMEM:
7899653d4eschrock		return (dgettext(TEXT_DOMAIN, "out of memory"));
7999653d4eschrock	case EZFS_BADPROP:
8099653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid property value"));
8199653d4eschrock	case EZFS_PROPREADONLY:
82f9af39bGeorge Wilson		return (dgettext(TEXT_DOMAIN, "read-only property"));
8399653d4eschrock	case EZFS_PROPTYPE:
8499653d4eschrock		return (dgettext(TEXT_DOMAIN, "property doesn't apply to "
8599653d4eschrock		    "datasets of this type"));
8699653d4eschrock	case EZFS_PROPNONINHERIT:
8799653d4eschrock		return (dgettext(TEXT_DOMAIN, "property cannot be inherited"));
8899653d4eschrock	case EZFS_PROPSPACE:
8999653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid quota or reservation"));
9099653d4eschrock	case EZFS_BADTYPE:
9199653d4eschrock		return (dgettext(TEXT_DOMAIN, "operation not applicable to "
9299653d4eschrock		    "datasets of this type"));
9399653d4eschrock	case EZFS_BUSY:
9499653d4eschrock		return (dgettext(TEXT_DOMAIN, "pool or dataset is busy"));
9599653d4eschrock	case EZFS_EXISTS:
9699653d4eschrock		return (dgettext(TEXT_DOMAIN, "pool or dataset exists"));
9799653d4eschrock	case EZFS_NOENT:
9899653d4eschrock		return (dgettext(TEXT_DOMAIN, "no such pool or dataset"));
9999653d4eschrock	case EZFS_BADSTREAM:
10099653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid backup stream"));
10199653d4eschrock	case EZFS_DSREADONLY:
102f9af39bGeorge Wilson		return (dgettext(TEXT_DOMAIN, "dataset is read-only"));
10399653d4eschrock	case EZFS_VOLTOOBIG:
10499653d4eschrock		return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for "
10599653d4eschrock		    "this system"));
10699653d4eschrock	case EZFS_INVALIDNAME:
10799653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid name"));
10899653d4eschrock	case EZFS_BADRESTORE:
10999653d4eschrock		return (dgettext(TEXT_DOMAIN, "unable to restore to "
11099653d4eschrock		    "destination"));
11199653d4eschrock	case EZFS_BADBACKUP:
11299653d4eschrock		return (dgettext(TEXT_DOMAIN, "backup failed"));
11399653d4eschrock	case EZFS_BADTARGET:
11499653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid target vdev"));
11599653d4eschrock	case EZFS_NODEVICE:
11699653d4eschrock		return (dgettext(TEXT_DOMAIN, "no such device in pool"));
11799653d4eschrock	case EZFS_BADDEV:
11899653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid device"));
11999653d4eschrock	case EZFS_NOREPLICAS:
12099653d4eschrock		return (dgettext(TEXT_DOMAIN, "no valid replicas"));
12199653d4eschrock	case EZFS_RESILVERING:
12299653d4eschrock		return (dgettext(TEXT_DOMAIN, "currently resilvering"));
12399653d4eschrock	case EZFS_BADVERSION:
124ad135b5Christopher Siden		return (dgettext(TEXT_DOMAIN, "unsupported version or "
125ad135b5Christopher Siden		    "feature"));
12699653d4eschrock	case EZFS_POOLUNAVAIL:
12799653d4eschrock		return (dgettext(TEXT_DOMAIN, "pool is unavailable"));
12899653d4eschrock	case EZFS_DEVOVERFLOW:
12999653d4eschrock		return (dgettext(TEXT_DOMAIN, "too many devices in one vdev"));
13099653d4eschrock	case EZFS_BADPATH:
13199653d4eschrock		return (dgettext(TEXT_DOMAIN, "must be an absolute path"));
13299653d4eschrock	case EZFS_CROSSTARGET:
13399653d4eschrock		return (dgettext(TEXT_DOMAIN, "operation crosses datasets or "
13499653d4eschrock		    "pools"));
13599653d4eschrock	case EZFS_ZONED:
13699653d4eschrock		return (dgettext(TEXT_DOMAIN, "dataset in use by local zone"));
13799653d4eschrock	case EZFS_MOUNTFAILED:
13899653d4eschrock		return (dgettext(TEXT_DOMAIN, "mount failed"));
13999653d4eschrock	case EZFS_UMOUNTFAILED:
14099653d4eschrock		return (dgettext(TEXT_DOMAIN, "umount failed"));
141f3861e1ahl	case EZFS_UNSHARENFSFAILED:
14299653d4eschrock		return (dgettext(TEXT_DOMAIN, "unshare(1M) failed"));
143f3861e1ahl	case EZFS_SHARENFSFAILED:
14499653d4eschrock		return (dgettext(TEXT_DOMAIN, "share(1M) failed"));
145da6c28aamw	case EZFS_UNSHARESMBFAILED:
146da6c28aamw		return (dgettext(TEXT_DOMAIN, "smb remove share failed"));
147da6c28aamw	case EZFS_SHARESMBFAILED:
148da6c28aamw		return (dgettext(TEXT_DOMAIN, "smb add share failed"));
14999653d4eschrock	case EZFS_PERM:
15099653d4eschrock		return (dgettext(TEXT_DOMAIN, "permission denied"));
15199653d4eschrock	case EZFS_NOSPC:
15299653d4eschrock		return (dgettext(TEXT_DOMAIN, "out of space"));
1536e27f86Sam Falkner	case EZFS_FAULT:
1546e27f86Sam Falkner		return (dgettext(TEXT_DOMAIN, "bad address"));
15599653d4eschrock	case EZFS_IO:
15699653d4eschrock		return (dgettext(TEXT_DOMAIN, "I/O error"));
15799653d4eschrock	case EZFS_INTR:
15899653d4eschrock		return (dgettext(TEXT_DOMAIN, "signal received"));
15999653d4eschrock	case EZFS_ISSPARE:
16099653d4eschrock		return (dgettext(TEXT_DOMAIN, "device is reserved as a hot "
16199653d4eschrock		    "spare"));
16299653d4eschrock	case EZFS_INVALCONFIG:
16399653d4eschrock		return (dgettext(TEXT_DOMAIN, "invalid vdev configuration"));
1643bb79beeschrock	case EZFS_RECURSIVE:
1653bb79beeschrock		return (dgettext(TEXT_DOMAIN, "recursive dataset dependency"));
16606eeb2aek	case EZFS_NOHISTORY:
16706eeb2aek		return (dgettext(TEXT_DOMAIN, "no history available"));
168b1b8ab3lling	case EZFS_POOLPROPS:
169b1b8ab3lling		return (dgettext(TEXT_DOMAIN, "failed to retrieve "
170b1b8ab3lling		    "pool properties"));
171b1b8ab3lling	case EZFS_POOL_NOTSUP:
172b1b8ab3lling		return (dgettext(TEXT_DOMAIN, "operation not supported "
173b1b8ab3lling		    "on this type of pool"));
174b1b8ab3lling	case EZFS_POOL_INVALARG:
175b1b8ab3lling		return (dgettext(TEXT_DOMAIN, "invalid argument for "
176b1b8ab3lling		    "this pool operation"));
177b7661ccmmusante	case EZFS_NAMETOOLONG:
178b7661ccmmusante		return (dgettext(TEXT_DOMAIN, "dataset name is too long"));
1798488aebtaylor	case EZFS_OPENFAILED:
1808488aebtaylor		return (dgettext(TEXT_DOMAIN, "open failed"));
1818488aebtaylor	case EZFS_NOCAP:
1828488aebtaylor		return (dgettext(TEXT_DOMAIN,
1838488aebtaylor		    "disk capacity information could not be retrieved"));
1848488aebtaylor	case EZFS_LABELFAILED:
1858488aebtaylor		return (dgettext(TEXT_DOMAIN, "write of label failed"));
186ecd6cf8marks	case EZFS_BADWHO:
187ecd6cf8marks		return (dgettext(TEXT_DOMAIN, "invalid user/group"));
188ecd6cf8marks	case EZFS_BADPERM:
189ecd6cf8marks		return (dgettext(TEXT_DOMAIN, "invalid permission"));
190ecd6cf8marks	case EZFS_BADPERMSET:
191ecd6cf8marks		return (dgettext(TEXT_DOMAIN, "invalid permission set name"));
192ecd6cf8marks	case EZFS_NODELEGATION:
193ecd6cf8marks		return (dgettext(TEXT_DOMAIN, "delegated administration is "
194ecd6cf8marks		    "disabled on pool"));
1952f8aaabeschrock	case EZFS_BADCACHE:
1962f8aaabeschrock		return (dgettext(TEXT_DOMAIN, "invalid or missing cache file"));
197fa94a07brendan	case EZFS_ISL2CACHE:
198fa94a07brendan		return (dgettext(TEXT_DOMAIN, "device is in use as a cache"));
199e7cbe64gw	case EZFS_VDEVNOTSUP:
200e7cbe64gw		return (dgettext(TEXT_DOMAIN, "vdev specification is not "
201e7cbe64gw		    "supported"));
20215e6edfgw	case EZFS_NOTSUP:
20315e6edfgw		return (dgettext(TEXT_DOMAIN, "operation not supported "
20415e6edfgw		    "on this dataset"));
2057ac8935Don Brady	case EZFS_IOC_NOTSUPPORTED:
2067ac8935Don Brady		return (dgettext(TEXT_DOMAIN, "operation not supported by "
2077ac8935Don Brady		    "zfs kernel module"));
20889a89eblling	case EZFS_ACTIVE_SPARE:
20989a89eblling		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
21089a89eblling		    "device"));
211e6ca193George Wilson	case EZFS_UNPLAYED_LOGS:
212e6ca193George Wilson		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
213e6ca193George Wilson		    "logs"));
214842727cChris Kirby	case EZFS_REFTAG_RELE:
215842727cChris Kirby		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
216842727cChris Kirby	case EZFS_REFTAG_HOLD:
217842727cChris Kirby		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
218842727cChris Kirby		    "dataset"));
219ca45db4Chris Kirby	case EZFS_TAGTOOLONG:
220ca45db4Chris Kirby		return (dgettext(TEXT_DOMAIN, "tag too long"));
2219e69d7dLori Alt	case EZFS_PIPEFAILED:
2229e69d7dLori Alt		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
2239e69d7dLori Alt	case EZFS_THREADCREATEFAILED:
2249e69d7dLori Alt		return (dgettext(TEXT_DOMAIN, "thread create failed"));
2251195e68Mark J Musante	case EZFS_POSTSPLIT_ONLINE:
2261195e68Mark J Musante		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
2271195e68Mark J Musante		    "into a new one"));
2281702cceAlek Pinchuk	case EZFS_SCRUB_PAUSED:
2291702cceAlek Pinchuk		return (dgettext(TEXT_DOMAIN, "scrub is paused; "
2301702cceAlek Pinchuk		    "use 'zpool scrub' to resume"));
2313f9d6adLin Ling	case EZFS_SCRUBBING:
2323f9d6adLin Ling		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
2333f9d6adLin Ling		    "use 'zpool scrub -s' to cancel current scrub"));
2343f9d6adLin Ling	case EZFS_NO_SCRUB:
2353f9d6adLin Ling		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
23699d5e17Tim Haley	case EZFS_DIFF:
23799d5e17Tim Haley		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
23899d5e17Tim Haley	case EZFS_DIFFDATA:
23999d5e17Tim Haley		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
240f9af39bGeorge Wilson	case EZFS_POOLREADONLY:
241f9af39bGeorge Wilson		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
2425cabbc6Prashanth Sreenivasa	case EZFS_NO_PENDING:
2435cabbc6Prashanth Sreenivasa		return (dgettext(TEXT_DOMAIN, "operation is not "
2445cabbc6Prashanth Sreenivasa		    "in progress"));
2458671400Serapheim Dimitropoulos	case EZFS_CHECKPOINT_EXISTS:
2468671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
2478671400Serapheim Dimitropoulos	case EZFS_DISCARDING_CHECKPOINT:
2488671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "currently discarding "
2498671400Serapheim Dimitropoulos		    "checkpoint"));
2508671400Serapheim Dimitropoulos	case EZFS_NO_CHECKPOINT:
2518671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
2528671400Serapheim Dimitropoulos	case EZFS_DEVRM_IN_PROGRESS:
2538671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "device removal in progress"));
2548671400Serapheim Dimitropoulos	case EZFS_VDEV_TOO_BIG:
2558671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
256e0f1c0aOlaf Faaland	case EZFS_ACTIVE_POOL:
257e0f1c0aOlaf Faaland		return (dgettext(TEXT_DOMAIN, "pool is imported on a "
258e0f1c0aOlaf Faaland		    "different host"));
259084fd14Brian Behlendorf	case EZFS_CRYPTOFAILED:
260084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "encryption failure"));
261094e47eGeorge Wilson	case EZFS_TOOMANY:
262094e47eGeorge Wilson		return (dgettext(TEXT_DOMAIN, "argument list too long"));
263094e47eGeorge Wilson	case EZFS_INITIALIZING:
264094e47eGeorge Wilson		return (dgettext(TEXT_DOMAIN, "currently initializing"));
265094e47eGeorge Wilson	case EZFS_NO_INITIALIZE:
266094e47eGeorge Wilson		return (dgettext(TEXT_DOMAIN, "there is no active "
267094e47eGeorge Wilson		    "initialization"));
2686ccda74loli	case EZFS_WRONG_PARENT:
2696ccda74loli		return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
270084fd14Brian Behlendorf	case EZFS_TRIMMING:
271084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "currently trimming"));
272084fd14Brian Behlendorf	case EZFS_NO_TRIM:
273084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "there is no active trim"));
274084fd14Brian Behlendorf	case EZFS_TRIM_NOTSUP:
275084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "trim operations are not "
276084fd14Brian Behlendorf		    "supported by this device"));
277e4c795bTom Caputi	case EZFS_NO_RESILVER_DEFER:
278e4c795bTom Caputi		return (dgettext(TEXT_DOMAIN, "this action requires the "
279e4c795bTom Caputi		    "resilver_defer feature"));
28099653d4eschrock	case EZFS_UNKNOWN:
28199653d4eschrock		return (dgettext(TEXT_DOMAIN, "unknown error"));
28299653d4eschrock	default:
283c08432eeschrock		assert(hdl->libzfs_error == 0);
284c08432eeschrock		return (dgettext(TEXT_DOMAIN, "no error"));
28599653d4eschrock	}
28699653d4eschrock}
28799653d4eschrock
28899653d4eschrock/*PRINTFLIKE2*/
289fa9e406ahrensvoid
29099653d4eschrockzfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
291fa9e406ahrens{
292fa9e406ahrens	va_list ap;
293fa9e406ahrens
294fa9e406ahrens	va_start(ap, fmt);
295fa9e406ahrens
29699653d4eschrock	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
29799653d4eschrock	    fmt, ap);
29899653d4eschrock	hdl->libzfs_desc_active = 1;
29999653d4eschrock
30099653d4eschrock	va_end(ap);
30199653d4eschrock}
30299653d4eschrock
30399653d4eschrockstatic void
30499653d4eschrockzfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
30599653d4eschrock{
30699653d4eschrock	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
30799653d4eschrock	    fmt, ap);
30899653d4eschrock	hdl->libzfs_error = error;
30999653d4eschrock
31099653d4eschrock	if (hdl->libzfs_desc_active)
31199653d4eschrock		hdl->libzfs_desc_active = 0;
31299653d4eschrock	else
31399653d4eschrock		hdl->libzfs_desc[0] = '\0';
31499653d4eschrock
31599653d4eschrock	if (hdl->libzfs_printerr) {
31699653d4eschrock		if (error == EZFS_UNKNOWN) {
31799653d4eschrock			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
31899653d4eschrock			    "error: %s\n"), libzfs_error_description(hdl));
31999653d4eschrock			abort();
32099653d4eschrock		}
32199653d4eschrock
32299653d4eschrock		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
323b1b8ab3lling		    libzfs_error_description(hdl));
32499653d4eschrock		if (error == EZFS_NOMEM)
32599653d4eschrock			exit(1);
326fa9e406ahrens	}
32799653d4eschrock}
32899653d4eschrock
329ece3d9bllingint
330ece3d9bllingzfs_error(libzfs_handle_t *hdl, int error, const char *msg)
331ece3d9blling{
332ece3d9blling	return (zfs_error_fmt(hdl, error, "%s", msg));
333ece3d9blling}
334ece3d9blling
33599653d4eschrock/*PRINTFLIKE3*/
33699653d4eschrockint
337ece3d9bllingzfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
33899653d4eschrock{
33999653d4eschrock	va_list ap;
34099653d4eschrock
34199653d4eschrock	va_start(ap, fmt);
34299653d4eschrock
34399653d4eschrock	zfs_verror(hdl, error, fmt, ap);
344fa9e406ahrens
345fa9e406ahrens	va_end(ap);
34699653d4eschrock
34799653d4eschrock	return (-1);
348fa9e406ahrens}
349fa9e406ahrens
35099653d4eschrockstatic int
35199653d4eschrockzfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
35299653d4eschrock    va_list ap)
35399653d4eschrock{
35499653d4eschrock	switch (error) {
35599653d4eschrock	case EPERM:
35699653d4eschrock	case EACCES:
35799653d4eschrock		zfs_verror(hdl, EZFS_PERM, fmt, ap);
35899653d4eschrock		return (-1);
35999653d4eschrock
360ecd6cf8marks	case ECANCELED:
361ecd6cf8marks		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
362ecd6cf8marks		return (-1);
363ecd6cf8marks
36499653d4eschrock	case EIO:
36599653d4eschrock		zfs_verror(hdl, EZFS_IO, fmt, ap);
36699653d4eschrock		return (-1);
36799653d4eschrock
3686e27f86Sam Falkner	case EFAULT:
3696e27f86Sam Falkner		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
3706e27f86Sam Falkner		return (-1);
3716e27f86Sam Falkner
37299653d4eschrock	case EINTR:
37399653d4eschrock		zfs_verror(hdl, EZFS_INTR, fmt, ap);
37499653d4eschrock		return (-1);
37599653d4eschrock	}
37699653d4eschrock
37799653d4eschrock	return (0);
37899653d4eschrock}
37999653d4eschrock
380ece3d9bllingint
381ece3d9bllingzfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
382ece3d9blling{
383ece3d9blling	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
384ece3d9blling}
385ece3d9blling
38699653d4eschrock/*PRINTFLIKE3*/
38799653d4eschrockint
388ece3d9bllingzfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
389fa9e406ahrens{
390fa9e406ahrens	va_list ap;
391fa9e406ahrens
392fa9e406ahrens	va_start(ap, fmt);
393fa9e406ahrens
39499653d4eschrock	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
39599653d4eschrock		va_end(ap);
39699653d4eschrock		return (-1);
397fa9e406ahrens	}
398fa9e406ahrens
39999653d4eschrock	switch (error) {
40099653d4eschrock	case ENXIO:
40197d9e3ack	case ENODEV:
40219b94dfMatthew Ahrens	case EPIPE:
40399653d4eschrock		zfs_verror(hdl, EZFS_IO, fmt, ap);
40499653d4eschrock		break;
40599653d4eschrock
40699653d4eschrock	case ENOENT:
40799653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40899653d4eschrock		    "dataset does not exist"));
40999653d4eschrock		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
41099653d4eschrock		break;
41199653d4eschrock
41299653d4eschrock	case ENOSPC:
41399653d4eschrock	case EDQUOT:
41499653d4eschrock		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
41599653d4eschrock		return (-1);
41699653d4eschrock
41799653d4eschrock	case EEXIST:
41899653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41999653d4eschrock		    "dataset already exists"));
42099653d4eschrock		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
42199653d4eschrock		break;
42299653d4eschrock
42399653d4eschrock	case EBUSY:
42499653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42599653d4eschrock		    "dataset is busy"));
42699653d4eschrock		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
42799653d4eschrock		break;
428ecd6cf8marks	case EROFS:
429f9af39bGeorge Wilson		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
430ecd6cf8marks		break;
431b7661ccmmusante	case ENAMETOOLONG:
432b7661ccmmusante		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
433b7661ccmmusante		break;
43440ff396ck	case ENOTSUP:
43540ff396ck		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
43640ff396ck		break;
43754d692bGeorge Wilson	case EAGAIN:
43854d692bGeorge Wilson		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43954d692bGeorge Wilson		    "pool I/O is currently suspended"));
44054d692bGeorge Wilson		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
44154d692bGeorge Wilson		break;
442e0f1c0aOlaf Faaland	case EREMOTEIO:
443e0f1c0aOlaf Faaland		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
444e0f1c0aOlaf Faaland		break;
4457ac8935Don Brady	case ZFS_ERR_IOC_CMD_UNAVAIL:
4467ac8935Don Brady		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
4477ac8935Don Brady		    "module does not support this operation. A reboot may "
4487ac8935Don Brady		    "be required to enable this operation."));
4497ac8935Don Brady		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
4507ac8935Don Brady		break;
4517ac8935Don Brady	case ZFS_ERR_IOC_ARG_UNAVAIL:
4527ac8935Don Brady		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
4537ac8935Don Brady		    "module does not support an option for this operation. "
4547ac8935Don Brady		    "A reboot may be required to enable this option."));
4557ac8935Don Brady		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
4567ac8935Don Brady		break;
4577ac8935Don Brady	case ZFS_ERR_IOC_ARG_REQUIRED:
4587ac8935Don Brady	case ZFS_ERR_IOC_ARG_BADTYPE:
4597ac8935Don Brady		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
4607ac8935Don Brady		break;
46199653d4eschrock	default:
46292241e0Tom Erickson		zfs_error_aux(hdl, strerror(error));
46399653d4eschrock		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
46499653d4eschrock		break;
46599653d4eschrock	}
46699653d4eschrock
46799653d4eschrock	va_end(ap);
46899653d4eschrock	return (-1);
469fa9e406ahrens}
470fa9e406ahrens
471ece3d9bllingint
472ece3d9bllingzpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
473ece3d9blling{
474ece3d9blling	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
475ece3d9blling}
476ece3d9blling
47799653d4eschrock/*PRINTFLIKE3*/
47899653d4eschrockint
479ece3d9bllingzpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
480fa9e406ahrens{
48199653d4eschrock	va_list ap;
48299653d4eschrock
48399653d4eschrock	va_start(ap, fmt);
48499653d4eschrock
48599653d4eschrock	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
48699653d4eschrock		va_end(ap);
48799653d4eschrock		return (-1);
48899653d4eschrock	}
48999653d4eschrock
49099653d4eschrock	switch (error) {
49199653d4eschrock	case ENODEV:
49299653d4eschrock		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
49399653d4eschrock		break;
49499653d4eschrock
49599653d4eschrock	case ENOENT:
496b1b8ab3lling		zfs_error_aux(hdl,
497b1b8ab3lling		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
49899653d4eschrock		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
49999653d4eschrock		break;
50099653d4eschrock
50199653d4eschrock	case EEXIST:
50299653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
50399653d4eschrock		    "pool already exists"));
50499653d4eschrock		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
50599653d4eschrock		break;
50699653d4eschrock
50799653d4eschrock	case EBUSY:
50899653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
50943afaaaEric Schrock		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
51099653d4eschrock		break;
51199653d4eschrock
51299653d4eschrock	case ENXIO:
51399653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51499653d4eschrock		    "one or more devices is currently unavailable"));
51599653d4eschrock		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
51699653d4eschrock		break;
51799653d4eschrock
51899653d4eschrock	case ENAMETOOLONG:
51999653d4eschrock		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
52099653d4eschrock		break;
52199653d4eschrock
522b1b8ab3lling	case ENOTSUP:
523b1b8ab3lling		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
524b1b8ab3lling		break;
525b1b8ab3lling
526b1b8ab3lling	case EINVAL:
527b1b8ab3lling		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
528b1b8ab3lling		break;
529b1b8ab3lling
530ecd6cf8marks	case ENOSPC:
531ecd6cf8marks	case EDQUOT:
532ecd6cf8marks		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
533ecd6cf8marks		return (-1);
534f9af39bGeorge Wilson
53554d692bGeorge Wilson	case EAGAIN:
53654d692bGeorge Wilson		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
53754d692bGeorge Wilson		    "pool I/O is currently suspended"));
53854d692bGeorge Wilson		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
53954d692bGeorge Wilson		break;
540ecd6cf8marks
541f9af39bGeorge Wilson	case EROFS:
542f9af39bGeorge Wilson		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
543f9af39bGeorge Wilson		break;
5445cabbc6Prashanth Sreenivasa	/* There is no pending operation to cancel */
5455cabbc6Prashanth Sreenivasa	case ENOTACTIVE:
5465cabbc6Prashanth Sreenivasa		zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
5475cabbc6Prashanth Sreenivasa		break;
548e0f1c0aOlaf Faaland	case EREMOTEIO:
549e0f1c0aOlaf Faaland		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
550e0f1c0aOlaf Faaland		break;
5518671400Serapheim Dimitropoulos	case ZFS_ERR_CHECKPOINT_EXISTS:
5528671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
5538671400Serapheim Dimitropoulos		break;
5548671400Serapheim Dimitropoulos	case ZFS_ERR_DISCARDING_CHECKPOINT:
5558671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
5568671400Serapheim Dimitropoulos		break;
5578671400Serapheim Dimitropoulos	case ZFS_ERR_NO_CHECKPOINT:
5588671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
5598671400Serapheim Dimitropoulos		break;
5608671400Serapheim Dimitropoulos	case ZFS_ERR_DEVRM_IN_PROGRESS:
5618671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
5628671400Serapheim Dimitropoulos		break;
5638671400Serapheim Dimitropoulos	case ZFS_ERR_VDEV_TOO_BIG:
5648671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
5658671400Serapheim Dimitropoulos		break;
5667ac8935Don Brady	case ZFS_ERR_IOC_CMD_UNAVAIL:
5677ac8935Don Brady		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
5687ac8935Don Brady		    "module does not support this operation. A reboot may "
5697ac8935Don Brady		    "be required to enable this operation."));
5707ac8935Don Brady		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
5717ac8935Don Brady		break;
5727ac8935Don Brady	case ZFS_ERR_IOC_ARG_UNAVAIL:
5737ac8935Don Brady		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs "
5747ac8935Don Brady		    "module does not support an option for this operation. "
5757ac8935Don Brady		    "A reboot may be required to enable this option."));
5767ac8935Don Brady		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
5777ac8935Don Brady		break;
5787ac8935Don Brady	case ZFS_ERR_IOC_ARG_REQUIRED:
5797ac8935Don Brady	case ZFS_ERR_IOC_ARG_BADTYPE:
5807ac8935Don Brady		zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap);
5817ac8935Don Brady		break;
58299653d4eschrock	default:
58399653d4eschrock		zfs_error_aux(hdl, strerror(error));
58499653d4eschrock		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
58599653d4eschrock	}
58699653d4eschrock
58799653d4eschrock	va_end(ap);
58899653d4eschrock	return (-1);
589fa9e406ahrens}
590fa9e406ahrens
591fa9e406ahrens/*
592fa9e406ahrens * Display an out of memory error message and abort the current program.
593fa9e406ahrens */
59499653d4eschrockint
59599653d4eschrockno_memory(libzfs_handle_t *hdl)
596fa9e406ahrens{
59799653d4eschrock	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
598fa9e406ahrens}
599fa9e406ahrens
600fa9e406ahrens/*
601fa9e406ahrens * A safe form of malloc() which will die if the allocation fails.
602fa9e406ahrens */
603fa9e406ahrensvoid *
60499653d4eschrockzfs_alloc(libzfs_handle_t *hdl, size_t size)
605fa9e406ahrens{
606fa9e406ahrens	void *data;
607fa9e406ahrens
608fa9e406ahrens	if ((data = calloc(1, size)) == NULL)
60999653d4eschrock		(void) no_memory(hdl);
610fa9e406ahrens
611fa9e406ahrens	return (data);
612fa9e406ahrens}
613fa9e406ahrens
614fa9e406ahrens/*
61599d5e17Tim Haley * A safe form of asprintf() which will die if the allocation fails.
61699d5e17Tim Haley */
61799d5e17Tim Haley/*PRINTFLIKE2*/
61899d5e17Tim Haleychar *
61999d5e17Tim Haleyzfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
62099d5e17Tim Haley{
62199d5e17Tim Haley	va_list ap;
62299d5e17Tim Haley	char *ret;
62399d5e17Tim Haley	int err;
62499d5e17Tim Haley
62599d5e17Tim Haley	va_start(ap, fmt);
62699d5e17Tim Haley
62799d5e17Tim Haley	err = vasprintf(&ret, fmt, ap);
62899d5e17Tim Haley
62999d5e17Tim Haley	va_end(ap);
63099d5e17Tim Haley
63199d5e17Tim Haley	if (err < 0)
63299d5e17Tim Haley		(void) no_memory(hdl);
63399d5e17Tim Haley
63499d5e17Tim Haley	return (ret);
63599d5e17Tim Haley}
63699d5e17Tim Haley
63799d5e17Tim Haley/*
638e9dbad6eschrock * A safe form of realloc(), which also zeroes newly allocated space.
639e9dbad6eschrock */
640e9dbad6eschrockvoid *
641e9dbad6eschrockzfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
642e9dbad6eschrock{
643e9dbad6eschrock	void *ret;
644