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