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.
24e0f1c0aOlaf Faaland * Copyright 2019 Joyent, Inc.
258671400Serapheim Dimitropoulos * Copyright (c) 2011, 2017 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
34fa9e406ahrens#include <errno.h>
35fa9e406ahrens#include <fcntl.h>
36fa9e406ahrens#include <libintl.h>
37fa9e406ahrens#include <stdarg.h>
38fa9e406ahrens#include <stdio.h>
39fa9e406ahrens#include <stdlib.h>
40fa9e406ahrens#include <strings.h>
41fa9e406ahrens#include <unistd.h>
42990b485lling#include <ctype.h>
43990b485lling#include <math.h>
442bcf024Max Grossman#include <sys/filio.h>
45fa9e406ahrens#include <sys/mnttab.h>
465aba80dck#include <sys/mntent.h>
475aba80dck#include <sys/types.h>
480a05512Jason King#include <libcmdutils.h>
49fa9e406ahrens
50fa9e406ahrens#include <libzfs.h>
514445fffMatthew Ahrens#include <libzfs_core.h>
52fa9e406ahrens
53fa9e406ahrens#include "libzfs_impl.h"
5491ebeefahrens#include "zfs_prop.h"
55e0f1c0aOlaf Faaland#include "zfs_comutil.h"
56ad135b5Christopher Siden#include "zfeature_common.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"));
20589a89eblling	case EZFS_ACTIVE_SPARE:
20689a89eblling		return (dgettext(TEXT_DOMAIN, "pool has active shared spare "
20789a89eblling		    "device"));
208e6ca193George Wilson	case EZFS_UNPLAYED_LOGS:
209e6ca193George Wilson		return (dgettext(TEXT_DOMAIN, "log device has unplayed intent "
210e6ca193George Wilson		    "logs"));
211842727cChris Kirby	case EZFS_REFTAG_RELE:
212842727cChris Kirby		return (dgettext(TEXT_DOMAIN, "no such tag on this dataset"));
213842727cChris Kirby	case EZFS_REFTAG_HOLD:
214842727cChris Kirby		return (dgettext(TEXT_DOMAIN, "tag already exists on this "
215842727cChris Kirby		    "dataset"));
216ca45db4Chris Kirby	case EZFS_TAGTOOLONG:
217ca45db4Chris Kirby		return (dgettext(TEXT_DOMAIN, "tag too long"));
2189e69d7dLori Alt	case EZFS_PIPEFAILED:
2199e69d7dLori Alt		return (dgettext(TEXT_DOMAIN, "pipe create failed"));
2209e69d7dLori Alt	case EZFS_THREADCREATEFAILED:
2219e69d7dLori Alt		return (dgettext(TEXT_DOMAIN, "thread create failed"));
2221195e68Mark J Musante	case EZFS_POSTSPLIT_ONLINE:
2231195e68Mark J Musante		return (dgettext(TEXT_DOMAIN, "disk was split from this pool "
2241195e68Mark J Musante		    "into a new one"));
2251702cceAlek Pinchuk	case EZFS_SCRUB_PAUSED:
2261702cceAlek Pinchuk		return (dgettext(TEXT_DOMAIN, "scrub is paused; "
2271702cceAlek Pinchuk		    "use 'zpool scrub' to resume"));
2283f9d6adLin Ling	case EZFS_SCRUBBING:
2293f9d6adLin Ling		return (dgettext(TEXT_DOMAIN, "currently scrubbing; "
2303f9d6adLin Ling		    "use 'zpool scrub -s' to cancel current scrub"));
2313f9d6adLin Ling	case EZFS_NO_SCRUB:
2323f9d6adLin Ling		return (dgettext(TEXT_DOMAIN, "there is no active scrub"));
23399d5e17Tim Haley	case EZFS_DIFF:
23499d5e17Tim Haley		return (dgettext(TEXT_DOMAIN, "unable to generate diffs"));
23599d5e17Tim Haley	case EZFS_DIFFDATA:
23699d5e17Tim Haley		return (dgettext(TEXT_DOMAIN, "invalid diff data"));
237f9af39bGeorge Wilson	case EZFS_POOLREADONLY:
238f9af39bGeorge Wilson		return (dgettext(TEXT_DOMAIN, "pool is read-only"));
2395cabbc6Prashanth Sreenivasa	case EZFS_NO_PENDING:
2405cabbc6Prashanth Sreenivasa		return (dgettext(TEXT_DOMAIN, "operation is not "
2415cabbc6Prashanth Sreenivasa		    "in progress"));
2428671400Serapheim Dimitropoulos	case EZFS_CHECKPOINT_EXISTS:
2438671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "checkpoint exists"));
2448671400Serapheim Dimitropoulos	case EZFS_DISCARDING_CHECKPOINT:
2458671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "currently discarding "
2468671400Serapheim Dimitropoulos		    "checkpoint"));
2478671400Serapheim Dimitropoulos	case EZFS_NO_CHECKPOINT:
2488671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "checkpoint does not exist"));
2498671400Serapheim Dimitropoulos	case EZFS_DEVRM_IN_PROGRESS:
2508671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "device removal in progress"));
2518671400Serapheim Dimitropoulos	case EZFS_VDEV_TOO_BIG:
2528671400Serapheim Dimitropoulos		return (dgettext(TEXT_DOMAIN, "device exceeds supported size"));
253e0f1c0aOlaf Faaland	case EZFS_ACTIVE_POOL:
254e0f1c0aOlaf Faaland		return (dgettext(TEXT_DOMAIN, "pool is imported on a "
255e0f1c0aOlaf Faaland		    "different host"));
256084fd14Brian Behlendorf	case EZFS_CRYPTOFAILED:
257084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "encryption failure"));
258094e47eGeorge Wilson	case EZFS_TOOMANY:
259094e47eGeorge Wilson		return (dgettext(TEXT_DOMAIN, "argument list too long"));
260094e47eGeorge Wilson	case EZFS_INITIALIZING:
261094e47eGeorge Wilson		return (dgettext(TEXT_DOMAIN, "currently initializing"));
262094e47eGeorge Wilson	case EZFS_NO_INITIALIZE:
263094e47eGeorge Wilson		return (dgettext(TEXT_DOMAIN, "there is no active "
264094e47eGeorge Wilson		    "initialization"));
2656ccda74loli	case EZFS_WRONG_PARENT:
2666ccda74loli		return (dgettext(TEXT_DOMAIN, "invalid parent dataset"));
267084fd14Brian Behlendorf	case EZFS_TRIMMING:
268084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "currently trimming"));
269084fd14Brian Behlendorf	case EZFS_NO_TRIM:
270084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "there is no active trim"));
271084fd14Brian Behlendorf	case EZFS_TRIM_NOTSUP:
272084fd14Brian Behlendorf		return (dgettext(TEXT_DOMAIN, "trim operations are not "
273084fd14Brian Behlendorf		    "supported by this device"));
274e4c795bTom Caputi	case EZFS_NO_RESILVER_DEFER:
275e4c795bTom Caputi		return (dgettext(TEXT_DOMAIN, "this action requires the "
276e4c795bTom Caputi		    "resilver_defer feature"));
27799653d4eschrock	case EZFS_UNKNOWN:
27899653d4eschrock		return (dgettext(TEXT_DOMAIN, "unknown error"));
27999653d4eschrock	default:
280c08432eeschrock		assert(hdl->libzfs_error == 0);
281c08432eeschrock		return (dgettext(TEXT_DOMAIN, "no error"));
28299653d4eschrock	}
28399653d4eschrock}
28499653d4eschrock
28599653d4eschrock/*PRINTFLIKE2*/
286fa9e406ahrensvoid
28799653d4eschrockzfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...)
288fa9e406ahrens{
289fa9e406ahrens	va_list ap;
290fa9e406ahrens
291fa9e406ahrens	va_start(ap, fmt);
292fa9e406ahrens
29399653d4eschrock	(void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc),
29499653d4eschrock	    fmt, ap);
29599653d4eschrock	hdl->libzfs_desc_active = 1;
29699653d4eschrock
29799653d4eschrock	va_end(ap);
29899653d4eschrock}
29999653d4eschrock
30099653d4eschrockstatic void
30199653d4eschrockzfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap)
30299653d4eschrock{
30399653d4eschrock	(void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action),
30499653d4eschrock	    fmt, ap);
30599653d4eschrock	hdl->libzfs_error = error;
30699653d4eschrock
30799653d4eschrock	if (hdl->libzfs_desc_active)
30899653d4eschrock		hdl->libzfs_desc_active = 0;
30999653d4eschrock	else
31099653d4eschrock		hdl->libzfs_desc[0] = '\0';
31199653d4eschrock
31299653d4eschrock	if (hdl->libzfs_printerr) {
31399653d4eschrock		if (error == EZFS_UNKNOWN) {
31499653d4eschrock			(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal "
31599653d4eschrock			    "error: %s\n"), libzfs_error_description(hdl));
31699653d4eschrock			abort();
31799653d4eschrock		}
31899653d4eschrock
31999653d4eschrock		(void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action,
320b1b8ab3lling		    libzfs_error_description(hdl));
32199653d4eschrock		if (error == EZFS_NOMEM)
32299653d4eschrock			exit(1);
323fa9e406ahrens	}
32499653d4eschrock}
32599653d4eschrock
326ece3d9bllingint
327ece3d9bllingzfs_error(libzfs_handle_t *hdl, int error, const char *msg)
328ece3d9blling{
329ece3d9blling	return (zfs_error_fmt(hdl, error, "%s", msg));
330ece3d9blling}
331ece3d9blling
33299653d4eschrock/*PRINTFLIKE3*/
33399653d4eschrockint
334ece3d9bllingzfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
33599653d4eschrock{
33699653d4eschrock	va_list ap;
33799653d4eschrock
33899653d4eschrock	va_start(ap, fmt);
33999653d4eschrock
34099653d4eschrock	zfs_verror(hdl, error, fmt, ap);
341fa9e406ahrens
342fa9e406ahrens	va_end(ap);
34399653d4eschrock
34499653d4eschrock	return (-1);
345fa9e406ahrens}
346fa9e406ahrens
34799653d4eschrockstatic int
34899653d4eschrockzfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt,
34999653d4eschrock    va_list ap)
35099653d4eschrock{
35199653d4eschrock	switch (error) {
35299653d4eschrock	case EPERM:
35399653d4eschrock	case EACCES:
35499653d4eschrock		zfs_verror(hdl, EZFS_PERM, fmt, ap);
35599653d4eschrock		return (-1);
35699653d4eschrock
357ecd6cf8marks	case ECANCELED:
358ecd6cf8marks		zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap);
359ecd6cf8marks		return (-1);
360ecd6cf8marks
36199653d4eschrock	case EIO:
36299653d4eschrock		zfs_verror(hdl, EZFS_IO, fmt, ap);
36399653d4eschrock		return (-1);
36499653d4eschrock
3656e27f86Sam Falkner	case EFAULT:
3666e27f86Sam Falkner		zfs_verror(hdl, EZFS_FAULT, fmt, ap);
3676e27f86Sam Falkner		return (-1);
3686e27f86Sam Falkner
36999653d4eschrock	case EINTR:
37099653d4eschrock		zfs_verror(hdl, EZFS_INTR, fmt, ap);
37199653d4eschrock		return (-1);
37299653d4eschrock	}
37399653d4eschrock
37499653d4eschrock	return (0);
37599653d4eschrock}
37699653d4eschrock
377ece3d9bllingint
378ece3d9bllingzfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
379ece3d9blling{
380ece3d9blling	return (zfs_standard_error_fmt(hdl, error, "%s", msg));
381ece3d9blling}
382ece3d9blling
38399653d4eschrock/*PRINTFLIKE3*/
38499653d4eschrockint
385ece3d9bllingzfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
386fa9e406ahrens{
387fa9e406ahrens	va_list ap;
388fa9e406ahrens
389fa9e406ahrens	va_start(ap, fmt);
390fa9e406ahrens
39199653d4eschrock	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
39299653d4eschrock		va_end(ap);
39399653d4eschrock		return (-1);
394fa9e406ahrens	}
395fa9e406ahrens
39699653d4eschrock	switch (error) {
39799653d4eschrock	case ENXIO:
39897d9e3ack	case ENODEV:
39919b94dfMatthew Ahrens	case EPIPE:
40099653d4eschrock		zfs_verror(hdl, EZFS_IO, fmt, ap);
40199653d4eschrock		break;
40299653d4eschrock
40399653d4eschrock	case ENOENT:
40499653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40599653d4eschrock		    "dataset does not exist"));
40699653d4eschrock		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
40799653d4eschrock		break;
40899653d4eschrock
40999653d4eschrock	case ENOSPC:
41099653d4eschrock	case EDQUOT:
41199653d4eschrock		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
41299653d4eschrock		return (-1);
41399653d4eschrock
41499653d4eschrock	case EEXIST:
41599653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41699653d4eschrock		    "dataset already exists"));
41799653d4eschrock		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
41899653d4eschrock		break;
41999653d4eschrock
42099653d4eschrock	case EBUSY:
42199653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42299653d4eschrock		    "dataset is busy"));
42399653d4eschrock		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
42499653d4eschrock		break;
425ecd6cf8marks	case EROFS:
426f9af39bGeorge Wilson		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
427ecd6cf8marks		break;
428b7661ccmmusante	case ENAMETOOLONG:
429b7661ccmmusante		zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap);
430b7661ccmmusante		break;
43140ff396ck	case ENOTSUP:
43240ff396ck		zfs_verror(hdl, EZFS_BADVERSION, fmt, ap);
43340ff396ck		break;
43454d692bGeorge Wilson	case EAGAIN:
43554d692bGeorge Wilson		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43654d692bGeorge Wilson		    "pool I/O is currently suspended"));
43754d692bGeorge Wilson		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
43854d692bGeorge Wilson		break;
439e0f1c0aOlaf Faaland	case EREMOTEIO:
440e0f1c0aOlaf Faaland		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
441e0f1c0aOlaf Faaland		break;
44299653d4eschrock	default:
44392241e0Tom Erickson		zfs_error_aux(hdl, strerror(error));
44499653d4eschrock		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
44599653d4eschrock		break;
44699653d4eschrock	}
44799653d4eschrock
44899653d4eschrock	va_end(ap);
44999653d4eschrock	return (-1);
450fa9e406ahrens}
451fa9e406ahrens
452ece3d9bllingint
453ece3d9bllingzpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg)
454ece3d9blling{
455ece3d9blling	return (zpool_standard_error_fmt(hdl, error, "%s", msg));
456ece3d9blling}
457ece3d9blling
45899653d4eschrock/*PRINTFLIKE3*/
45999653d4eschrockint
460ece3d9bllingzpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...)
461fa9e406ahrens{
46299653d4eschrock	va_list ap;
46399653d4eschrock
46499653d4eschrock	va_start(ap, fmt);
46599653d4eschrock
46699653d4eschrock	if (zfs_common_error(hdl, error, fmt, ap) != 0) {
46799653d4eschrock		va_end(ap);
46899653d4eschrock		return (-1);
46999653d4eschrock	}
47099653d4eschrock
47199653d4eschrock	switch (error) {
47299653d4eschrock	case ENODEV:
47399653d4eschrock		zfs_verror(hdl, EZFS_NODEVICE, fmt, ap);
47499653d4eschrock		break;
47599653d4eschrock
47699653d4eschrock	case ENOENT:
477b1b8ab3lling		zfs_error_aux(hdl,
478b1b8ab3lling		    dgettext(TEXT_DOMAIN, "no such pool or dataset"));
47999653d4eschrock		zfs_verror(hdl, EZFS_NOENT, fmt, ap);
48099653d4eschrock		break;
48199653d4eschrock
48299653d4eschrock	case EEXIST:
48399653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
48499653d4eschrock		    "pool already exists"));
48599653d4eschrock		zfs_verror(hdl, EZFS_EXISTS, fmt, ap);
48699653d4eschrock		break;
48799653d4eschrock
48899653d4eschrock	case EBUSY:
48999653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy"));
49043afaaaEric Schrock		zfs_verror(hdl, EZFS_BUSY, fmt, ap);
49199653d4eschrock		break;
49299653d4eschrock
49399653d4eschrock	case ENXIO:
49499653d4eschrock		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
49599653d4eschrock		    "one or more devices is currently unavailable"));
49699653d4eschrock		zfs_verror(hdl, EZFS_BADDEV, fmt, ap);
49799653d4eschrock		break;
49899653d4eschrock
49999653d4eschrock	case ENAMETOOLONG:
50099653d4eschrock		zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap);
50199653d4eschrock		break;
50299653d4eschrock
503b1b8ab3lling	case ENOTSUP:
504b1b8ab3lling		zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap);
505b1b8ab3lling		break;
506b1b8ab3lling
507b1b8ab3lling	case EINVAL:
508b1b8ab3lling		zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap);
509b1b8ab3lling		break;
510b1b8ab3lling
511ecd6cf8marks	case ENOSPC:
512ecd6cf8marks	case EDQUOT:
513ecd6cf8marks		zfs_verror(hdl, EZFS_NOSPC, fmt, ap);
514ecd6cf8marks		return (-1);
515f9af39bGeorge Wilson
51654d692bGeorge Wilson	case EAGAIN:
51754d692bGeorge Wilson		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
51854d692bGeorge Wilson		    "pool I/O is currently suspended"));
51954d692bGeorge Wilson		zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap);
52054d692bGeorge Wilson		break;
521ecd6cf8marks
522f9af39bGeorge Wilson	case EROFS:
523f9af39bGeorge Wilson		zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap);
524f9af39bGeorge Wilson		break;
5255cabbc6Prashanth Sreenivasa	/* There is no pending operation to cancel */
5265cabbc6Prashanth Sreenivasa	case ENOTACTIVE:
5275cabbc6Prashanth Sreenivasa		zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap);
5285cabbc6Prashanth Sreenivasa		break;
529e0f1c0aOlaf Faaland	case EREMOTEIO:
530e0f1c0aOlaf Faaland		zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap);
531e0f1c0aOlaf Faaland		break;
5328671400Serapheim Dimitropoulos	case ZFS_ERR_CHECKPOINT_EXISTS:
5338671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap);
5348671400Serapheim Dimitropoulos		break;
5358671400Serapheim Dimitropoulos	case ZFS_ERR_DISCARDING_CHECKPOINT:
5368671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap);
5378671400Serapheim Dimitropoulos		break;
5388671400Serapheim Dimitropoulos	case ZFS_ERR_NO_CHECKPOINT:
5398671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap);
5408671400Serapheim Dimitropoulos		break;
5418671400Serapheim Dimitropoulos	case ZFS_ERR_DEVRM_IN_PROGRESS:
5428671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap);
5438671400Serapheim Dimitropoulos		break;
5448671400Serapheim Dimitropoulos	case ZFS_ERR_VDEV_TOO_BIG:
5458671400Serapheim Dimitropoulos		zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap);
5468671400Serapheim Dimitropoulos		break;
54799653d4eschrock	default:
54899653d4eschrock		zfs_error_aux(hdl, strerror(error));
54999653d4eschrock		zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap);
55099653d4eschrock	}
55199653d4eschrock
55299653d4eschrock	va_end(ap);
55399653d4eschrock	return (-1);
554fa9e406ahrens}
555fa9e406ahrens
556fa9e406ahrens/*
557fa9e406ahrens * Display an out of memory error message and abort the current program.
558fa9e406ahrens */
55999653d4eschrockint
56099653d4eschrockno_memory(libzfs_handle_t *hdl)
561fa9e406ahrens{
56299653d4eschrock	return (zfs_error(hdl, EZFS_NOMEM, "internal error"));
563fa9e406ahrens}
564fa9e406ahrens
565fa9e406ahrens/*
566fa9e406ahrens * A safe form of malloc() which will die if the allocation fails.
567fa9e406ahrens */
568fa9e406ahrensvoid *
56999653d4eschrockzfs_alloc(libzfs_handle_t *hdl, size_t size)
570fa9e406ahrens{
571fa9e406ahrens	void *data;
572fa9e406ahrens
573fa9e406ahrens	if ((data = calloc(1, size)) == NULL)
57499653d4eschrock		(void) no_memory(hdl);
575fa9e406ahrens
576fa9e406ahrens	return (data);
577fa9e406ahrens}
578fa9e406ahrens
579fa9e406ahrens/*
58099d5e17Tim Haley * A safe form of asprintf() which will die if the allocation fails.
58199d5e17Tim Haley */
58299d5e17Tim Haley/*PRINTFLIKE2*/
58399d5e17Tim Haleychar *
58499d5e17Tim Haleyzfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...)
58599d5e17Tim Haley{
58699d5e17Tim Haley	va_list ap;
58799d5e17Tim Haley	char *ret;
58899d5e17Tim Haley	int err;
58999d5e17Tim Haley
59099d5e17Tim Haley	va_start(ap, fmt);
59199d5e17Tim Haley
59299d5e17Tim Haley	err = vasprintf(&ret, fmt, ap);
59399d5e17Tim Haley
59499d5e17Tim Haley	va_end(ap);
59599d5e17Tim Haley
59699d5e17Tim Haley	if (err < 0)
59799d5e17Tim Haley		(void) no_memory(hdl);
59899d5e17Tim Haley
59999d5e17Tim Haley	return (ret);
60099d5e17Tim Haley}
60199d5e17Tim Haley
60299d5e17Tim Haley/*
603e9dbad6eschrock * A safe form of realloc(), which also zeroes newly allocated space.
604e9dbad6eschrock */
605e9dbad6eschrockvoid *
606e9dbad6eschrockzfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize)
607e9dbad6eschrock{
608e9dbad6eschrock	void *ret;
609e9dbad6eschrock
610e9dbad6eschrock	if ((ret = realloc(ptr, newsize)) == NULL) {
611e9dbad6eschrock		(void) no_memory(hdl);
612e9dbad6eschrock		return (NULL);
613e9dbad6eschrock	}
614e9dbad6eschrock
615e9dbad6eschrock	bzero((char *)ret + oldsize, (newsize - oldsize));
616e9dbad6eschrock	return (ret);
617e9dbad6eschrock}
618e9dbad6eschrock
619e9dbad6eschrock/*
620fa9e406ahrens * A safe form of strdup() which will die if the allocation fails.
621fa9e406ahrens */
622fa9e406ahrenschar *
62399653d4eschrockzfs_strdup(libzfs_handle_t *hdl, const char *str)
624fa9e406ahrens{
625fa9e406ahrens	char *ret;
626fa9e406ahrens
627fa9e406ahrens	if ((ret = strdup(str)) == NULL)
62899653d4eschrock		(void) no_memory(hdl);
629fa9e406ahrens
630fa9e406ahrens	return (ret);
631fa9e406ahrens}
632fa9e406ahrens
633fa9e406ahrens/*
634fa9e406ahrens * Convert a number to an appropriately human-readable output.
635fa9e406ahrens */
636fa9e406ahrensvoid
637fa9e406ahrenszfs_nicenum(uint64_t num, char *buf, size_t buflen)
638fa9e406ahrens{
6390a05512Jason King	nicenum(num, buf, buflen);
640fa9e406ahrens}
64199653d4eschrock
642