xref: /linux/fs/bcachefs/errcode.c (revision ff0905bbf991f4337b5ebc19c0d43525ebb0d96b)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "errcode.h"
5 #include "trace.h"
6 
7 #include <linux/errname.h>
8 
9 static const char * const bch2_errcode_strs[] = {
10 #define x(class, err) [BCH_ERR_##err - BCH_ERR_START] = #err,
11 	BCH_ERRCODES()
12 #undef x
13 	NULL
14 };
15 
16 static const unsigned bch2_errcode_parents[] = {
17 #define x(class, err) [BCH_ERR_##err - BCH_ERR_START] = class,
18 	BCH_ERRCODES()
19 #undef x
20 };
21 
22 __attribute__((const))
bch2_err_str(int err)23 const char *bch2_err_str(int err)
24 {
25 	const char *errstr;
26 
27 	err = abs(err);
28 
29 	BUG_ON(err >= BCH_ERR_MAX);
30 
31 	if (err >= BCH_ERR_START)
32 		errstr = bch2_errcode_strs[err - BCH_ERR_START];
33 	else if (err)
34 		errstr = errname(err);
35 	else
36 		errstr = "(No error)";
37 	return errstr ?: "(Invalid error)";
38 }
39 
40 __attribute__((const))
__bch2_err_matches(int err,int class)41 bool __bch2_err_matches(int err, int class)
42 {
43 	err	= abs(err);
44 	class	= abs(class);
45 
46 	BUG_ON(err	>= BCH_ERR_MAX);
47 	BUG_ON(class	>= BCH_ERR_MAX);
48 
49 	while (err >= BCH_ERR_START && err != class)
50 		err = bch2_errcode_parents[err - BCH_ERR_START];
51 
52 	return err == class;
53 }
54 
__bch2_err_class(int bch_err)55 int __bch2_err_class(int bch_err)
56 {
57 	int std_err = -bch_err;
58 	BUG_ON((unsigned) std_err >= BCH_ERR_MAX);
59 
60 	while (std_err >= BCH_ERR_START && bch2_errcode_parents[std_err - BCH_ERR_START])
61 		std_err = bch2_errcode_parents[std_err - BCH_ERR_START];
62 
63 	trace_error_downcast(bch_err, std_err, _RET_IP_);
64 
65 	return -std_err;
66 }
67 
bch2_blk_status_to_str(blk_status_t status)68 const char *bch2_blk_status_to_str(blk_status_t status)
69 {
70 	if (status == BLK_STS_REMOVED)
71 		return "device removed";
72 	return blk_status_to_str(status);
73 }
74