xref: /src/lib/libbe/be_error.c (revision 54d2737e7fe48226c908dcccfbda2ca1c08e07fc)
154d2737eSAlexander Ziaee /*
228f16a0fSKyle Evans  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
328f16a0fSKyle Evans  *
454d2737eSAlexander Ziaee  * SPDX-License-Identifier: BSD-2-Clause
528f16a0fSKyle Evans  */
628f16a0fSKyle Evans 
7b6e7c421SKyle Evans #include <sys/cdefs.h>
828f16a0fSKyle Evans #include "be.h"
928f16a0fSKyle Evans #include "be_impl.h"
1028f16a0fSKyle Evans 
1128f16a0fSKyle Evans /*
1228f16a0fSKyle Evans  * Usage
1328f16a0fSKyle Evans  */
1428f16a0fSKyle Evans int
libbe_errno(libbe_handle_t * lbh)1528f16a0fSKyle Evans libbe_errno(libbe_handle_t *lbh)
1628f16a0fSKyle Evans {
17bfe0869cSKyle Evans 
1828f16a0fSKyle Evans 	return (lbh->error);
1928f16a0fSKyle Evans }
2028f16a0fSKyle Evans 
2128f16a0fSKyle Evans 
2228f16a0fSKyle Evans const char *
libbe_error_description(libbe_handle_t * lbh)2328f16a0fSKyle Evans libbe_error_description(libbe_handle_t *lbh)
2428f16a0fSKyle Evans {
25bfe0869cSKyle Evans 
2628f16a0fSKyle Evans 	switch (lbh->error) {
2728f16a0fSKyle Evans 	case BE_ERR_INVALIDNAME:
2828f16a0fSKyle Evans 		return ("invalid boot environment name");
2928f16a0fSKyle Evans 
3028f16a0fSKyle Evans 	case BE_ERR_EXISTS:
3128f16a0fSKyle Evans 		return ("boot environment name already taken");
3228f16a0fSKyle Evans 
3328f16a0fSKyle Evans 	case BE_ERR_NOENT:
3428f16a0fSKyle Evans 		return ("specified boot environment does not exist");
3528f16a0fSKyle Evans 
3628f16a0fSKyle Evans 	case BE_ERR_PERMS:
3728f16a0fSKyle Evans 		return ("insufficient permissions");
3828f16a0fSKyle Evans 
3928f16a0fSKyle Evans 	case BE_ERR_DESTROYACT:
4028f16a0fSKyle Evans 		return ("cannot destroy active boot environment");
4128f16a0fSKyle Evans 
4228f16a0fSKyle Evans 	case BE_ERR_DESTROYMNT:
4328f16a0fSKyle Evans 		return ("cannot destroy mounted boot env unless forced");
4428f16a0fSKyle Evans 
4550a1972eSKyle Evans 	case BE_ERR_BADPATH:
4650a1972eSKyle Evans 		return ("path not suitable for operation");
4750a1972eSKyle Evans 
4850a1972eSKyle Evans 	case BE_ERR_PATHBUSY:
4950a1972eSKyle Evans 		return ("specified path is busy");
5050a1972eSKyle Evans 
5128f16a0fSKyle Evans 	case BE_ERR_PATHLEN:
5228f16a0fSKyle Evans 		return ("provided path name exceeds maximum length limit");
5328f16a0fSKyle Evans 
54162ec569SKyle Evans 	case BE_ERR_BADMOUNT:
55162ec569SKyle Evans 		return ("mountpoint is not \"/\"");
5628f16a0fSKyle Evans 
5728f16a0fSKyle Evans 	case BE_ERR_NOORIGIN:
5828f16a0fSKyle Evans 		return ("could not open snapshot's origin");
5928f16a0fSKyle Evans 
6028f16a0fSKyle Evans 	case BE_ERR_MOUNTED:
6128f16a0fSKyle Evans 		return ("boot environment is already mounted");
6228f16a0fSKyle Evans 
6328f16a0fSKyle Evans 	case BE_ERR_NOMOUNT:
6428f16a0fSKyle Evans 		return ("boot environment is not mounted");
6528f16a0fSKyle Evans 
6628f16a0fSKyle Evans 	case BE_ERR_ZFSOPEN:
6728f16a0fSKyle Evans 		return ("calling zfs_open() failed");
6828f16a0fSKyle Evans 
6928f16a0fSKyle Evans 	case BE_ERR_ZFSCLONE:
7028f16a0fSKyle Evans 		return ("error when calling zfs_clone() to create boot env");
7128f16a0fSKyle Evans 
7250a1972eSKyle Evans 	case BE_ERR_IO:
7350a1972eSKyle Evans 		return ("input/output error");
7450a1972eSKyle Evans 
752989df09SKyle Evans 	case BE_ERR_NOPOOL:
762989df09SKyle Evans 		return ("operation not supported on this pool");
772989df09SKyle Evans 
78c65a2111SKyle Evans 	case BE_ERR_NOMEM:
79c65a2111SKyle Evans 		return ("insufficient memory");
80c65a2111SKyle Evans 
8128f16a0fSKyle Evans 	case BE_ERR_UNKNOWN:
8228f16a0fSKyle Evans 		return ("unknown error");
8328f16a0fSKyle Evans 
84be7dd423SKyle Evans 	case BE_ERR_INVORIGIN:
85be7dd423SKyle Evans 		return ("invalid origin");
86be7dd423SKyle Evans 
878f5c6c31SKyle Evans 	case BE_ERR_HASCLONES:
888f5c6c31SKyle Evans 		return ("snapshot has clones");
898f5c6c31SKyle Evans 
9028f16a0fSKyle Evans 	default:
9128f16a0fSKyle Evans 		assert(lbh->error == BE_ERR_SUCCESS);
9228f16a0fSKyle Evans 		return ("no error");
9328f16a0fSKyle Evans 	}
9428f16a0fSKyle Evans }
9528f16a0fSKyle Evans 
9628f16a0fSKyle Evans 
9728f16a0fSKyle Evans void
libbe_print_on_error(libbe_handle_t * lbh,bool val)9828f16a0fSKyle Evans libbe_print_on_error(libbe_handle_t *lbh, bool val)
9928f16a0fSKyle Evans {
100bfe0869cSKyle Evans 
10128f16a0fSKyle Evans 	lbh->print_on_err = val;
10228f16a0fSKyle Evans 	libzfs_print_on_error(lbh->lzh, val);
10328f16a0fSKyle Evans }
10428f16a0fSKyle Evans 
10528f16a0fSKyle Evans 
10628f16a0fSKyle Evans int
set_error(libbe_handle_t * lbh,be_error_t err)10728f16a0fSKyle Evans set_error(libbe_handle_t *lbh, be_error_t err)
10828f16a0fSKyle Evans {
10928f16a0fSKyle Evans 
11028f16a0fSKyle Evans 	lbh->error = err;
111bfe0869cSKyle Evans 	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
11228f16a0fSKyle Evans 		fprintf(stderr, "%s\n", libbe_error_description(lbh));
11328f16a0fSKyle Evans 
11428f16a0fSKyle Evans 	return (err);
11528f16a0fSKyle Evans }
116