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