xref: /freebsd/lib/libbe/be_error.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1b179da01SKyle Evans /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
328f16a0fSKyle Evans  *
428f16a0fSKyle Evans  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
528f16a0fSKyle Evans  *
628f16a0fSKyle Evans  * Redistribution and use in source and binary forms, with or without
728f16a0fSKyle Evans  * modification, are permitted provided that the following conditions
828f16a0fSKyle Evans  * are met:
928f16a0fSKyle Evans  * 1. Redistributions of source code must retain the above copyright
1028f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer.
1128f16a0fSKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
1228f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer in the
1328f16a0fSKyle Evans  *    documentation and/or other materials provided with the distribution.
1428f16a0fSKyle Evans  *
1528f16a0fSKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1628f16a0fSKyle Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1728f16a0fSKyle Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1828f16a0fSKyle Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1928f16a0fSKyle Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2028f16a0fSKyle Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2128f16a0fSKyle Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2228f16a0fSKyle Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2328f16a0fSKyle Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2428f16a0fSKyle Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2528f16a0fSKyle Evans  * SUCH DAMAGE.
2628f16a0fSKyle Evans  */
2728f16a0fSKyle Evans 
28b6e7c421SKyle Evans #include <sys/cdefs.h>
29b6e7c421SKyle Evans __FBSDID("$FreeBSD$");
30b6e7c421SKyle Evans 
3128f16a0fSKyle Evans #include "be.h"
3228f16a0fSKyle Evans #include "be_impl.h"
3328f16a0fSKyle Evans 
3428f16a0fSKyle Evans /*
3528f16a0fSKyle Evans  * Usage
3628f16a0fSKyle Evans  */
3728f16a0fSKyle Evans int
3828f16a0fSKyle Evans libbe_errno(libbe_handle_t *lbh)
3928f16a0fSKyle Evans {
40bfe0869cSKyle Evans 
4128f16a0fSKyle Evans 	return (lbh->error);
4228f16a0fSKyle Evans }
4328f16a0fSKyle Evans 
4428f16a0fSKyle Evans 
4528f16a0fSKyle Evans const char *
4628f16a0fSKyle Evans libbe_error_description(libbe_handle_t *lbh)
4728f16a0fSKyle Evans {
48bfe0869cSKyle Evans 
4928f16a0fSKyle Evans 	switch (lbh->error) {
5028f16a0fSKyle Evans 	case BE_ERR_INVALIDNAME:
5128f16a0fSKyle Evans 		return ("invalid boot environment name");
5228f16a0fSKyle Evans 
5328f16a0fSKyle Evans 	case BE_ERR_EXISTS:
5428f16a0fSKyle Evans 		return ("boot environment name already taken");
5528f16a0fSKyle Evans 
5628f16a0fSKyle Evans 	case BE_ERR_NOENT:
5728f16a0fSKyle Evans 		return ("specified boot environment does not exist");
5828f16a0fSKyle Evans 
5928f16a0fSKyle Evans 	case BE_ERR_PERMS:
6028f16a0fSKyle Evans 		return ("insufficient permissions");
6128f16a0fSKyle Evans 
6228f16a0fSKyle Evans 	case BE_ERR_DESTROYACT:
6328f16a0fSKyle Evans 		return ("cannot destroy active boot environment");
6428f16a0fSKyle Evans 
6528f16a0fSKyle Evans 	case BE_ERR_DESTROYMNT:
6628f16a0fSKyle Evans 		return ("cannot destroy mounted boot env unless forced");
6728f16a0fSKyle Evans 
6850a1972eSKyle Evans 	case BE_ERR_BADPATH:
6950a1972eSKyle Evans 		return ("path not suitable for operation");
7050a1972eSKyle Evans 
7150a1972eSKyle Evans 	case BE_ERR_PATHBUSY:
7250a1972eSKyle Evans 		return ("specified path is busy");
7350a1972eSKyle Evans 
7428f16a0fSKyle Evans 	case BE_ERR_PATHLEN:
7528f16a0fSKyle Evans 		return ("provided path name exceeds maximum length limit");
7628f16a0fSKyle Evans 
77162ec569SKyle Evans 	case BE_ERR_BADMOUNT:
78162ec569SKyle Evans 		return ("mountpoint is not \"/\"");
7928f16a0fSKyle Evans 
8028f16a0fSKyle Evans 	case BE_ERR_NOORIGIN:
8128f16a0fSKyle Evans 		return ("could not open snapshot's origin");
8228f16a0fSKyle Evans 
8328f16a0fSKyle Evans 	case BE_ERR_MOUNTED:
8428f16a0fSKyle Evans 		return ("boot environment is already mounted");
8528f16a0fSKyle Evans 
8628f16a0fSKyle Evans 	case BE_ERR_NOMOUNT:
8728f16a0fSKyle Evans 		return ("boot environment is not mounted");
8828f16a0fSKyle Evans 
8928f16a0fSKyle Evans 	case BE_ERR_ZFSOPEN:
9028f16a0fSKyle Evans 		return ("calling zfs_open() failed");
9128f16a0fSKyle Evans 
9228f16a0fSKyle Evans 	case BE_ERR_ZFSCLONE:
9328f16a0fSKyle Evans 		return ("error when calling zfs_clone() to create boot env");
9428f16a0fSKyle Evans 
9550a1972eSKyle Evans 	case BE_ERR_IO:
9650a1972eSKyle Evans 		return ("input/output error");
9750a1972eSKyle Evans 
982989df09SKyle Evans 	case BE_ERR_NOPOOL:
992989df09SKyle Evans 		return ("operation not supported on this pool");
1002989df09SKyle Evans 
101c65a2111SKyle Evans 	case BE_ERR_NOMEM:
102c65a2111SKyle Evans 		return ("insufficient memory");
103c65a2111SKyle Evans 
10428f16a0fSKyle Evans 	case BE_ERR_UNKNOWN:
10528f16a0fSKyle Evans 		return ("unknown error");
10628f16a0fSKyle Evans 
107be7dd423SKyle Evans 	case BE_ERR_INVORIGIN:
108be7dd423SKyle Evans 		return ("invalid origin");
109be7dd423SKyle Evans 
1108f5c6c31SKyle Evans 	case BE_ERR_HASCLONES:
1118f5c6c31SKyle Evans 		return ("snapshot has clones");
1128f5c6c31SKyle Evans 
11328f16a0fSKyle Evans 	default:
11428f16a0fSKyle Evans 		assert(lbh->error == BE_ERR_SUCCESS);
11528f16a0fSKyle Evans 		return ("no error");
11628f16a0fSKyle Evans 	}
11728f16a0fSKyle Evans }
11828f16a0fSKyle Evans 
11928f16a0fSKyle Evans 
12028f16a0fSKyle Evans void
12128f16a0fSKyle Evans libbe_print_on_error(libbe_handle_t *lbh, bool val)
12228f16a0fSKyle Evans {
123bfe0869cSKyle Evans 
12428f16a0fSKyle Evans 	lbh->print_on_err = val;
12528f16a0fSKyle Evans 	libzfs_print_on_error(lbh->lzh, val);
12628f16a0fSKyle Evans }
12728f16a0fSKyle Evans 
12828f16a0fSKyle Evans 
12928f16a0fSKyle Evans int
13028f16a0fSKyle Evans set_error(libbe_handle_t *lbh, be_error_t err)
13128f16a0fSKyle Evans {
13228f16a0fSKyle Evans 
13328f16a0fSKyle Evans 	lbh->error = err;
134bfe0869cSKyle Evans 	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
13528f16a0fSKyle Evans 		fprintf(stderr, "%s\n", libbe_error_description(lbh));
13628f16a0fSKyle Evans 
13728f16a0fSKyle Evans 	return (err);
13828f16a0fSKyle Evans }
139