]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/errcode.c
Update bcachefs sources to 400f275d46 bcachefs: Fix check_overlapping_extents()
[bcachefs-tools-debian] / libbcachefs / errcode.c
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 #define BCH_ERR_0       0
16
17 static unsigned bch2_errcode_parents[] = {
18 #define x(class, err) [BCH_ERR_##err - BCH_ERR_START] = class,
19         BCH_ERRCODES()
20 #undef x
21 };
22
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 bool __bch2_err_matches(int err, int class)
41 {
42         err     = abs(err);
43         class   = abs(class);
44
45         BUG_ON(err      >= BCH_ERR_MAX);
46         BUG_ON(class    >= BCH_ERR_MAX);
47
48         while (err >= BCH_ERR_START && err != class)
49                 err = bch2_errcode_parents[err - BCH_ERR_START];
50
51         return err == class;
52 }
53
54 int __bch2_err_class(int err)
55 {
56         err = -err;
57         BUG_ON((unsigned) err >= BCH_ERR_MAX);
58
59         while (err >= BCH_ERR_START && bch2_errcode_parents[err - BCH_ERR_START])
60                 err = bch2_errcode_parents[err - BCH_ERR_START];
61
62         return -err;
63 }