]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.h
Update bcachefs sources to ca3cfad39f fixup! bcachefs: Improve iter->should_be_locked
[bcachefs-tools-debian] / libbcachefs / error.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ERROR_H
3 #define _BCACHEFS_ERROR_H
4
5 #include <linux/list.h>
6 #include <linux/printk.h>
7
8 struct bch_dev;
9 struct bch_fs;
10 struct work_struct;
11
12 /*
13  * XXX: separate out errors that indicate on disk data is inconsistent, and flag
14  * superblock as such
15  */
16
17 /* Error messages: */
18
19 /*
20  * Inconsistency errors: The on disk data is inconsistent. If these occur during
21  * initial recovery, they don't indicate a bug in the running code - we walk all
22  * the metadata before modifying anything. If they occur at runtime, they
23  * indicate either a bug in the running code or (less likely) data is being
24  * silently corrupted under us.
25  *
26  * XXX: audit all inconsistent errors and make sure they're all recoverable, in
27  * BCH_ON_ERROR_CONTINUE mode
28  */
29
30 bool bch2_inconsistent_error(struct bch_fs *);
31
32 void bch2_topology_error(struct bch_fs *);
33
34 #define bch2_fs_inconsistent(c, ...)                                    \
35 ({                                                                      \
36         bch_err(c, __VA_ARGS__);                                        \
37         bch2_inconsistent_error(c);                                     \
38 })
39
40 #define bch2_fs_inconsistent_on(cond, c, ...)                           \
41 ({                                                                      \
42         int _ret = !!(cond);                                            \
43                                                                         \
44         if (_ret)                                                       \
45                 bch2_fs_inconsistent(c, __VA_ARGS__);                   \
46         _ret;                                                           \
47 })
48
49 /*
50  * Later we might want to mark only the particular device inconsistent, not the
51  * entire filesystem:
52  */
53
54 #define bch2_dev_inconsistent(ca, ...)                                  \
55 do {                                                                    \
56         bch_err(ca, __VA_ARGS__);                                       \
57         bch2_inconsistent_error((ca)->fs);                              \
58 } while (0)
59
60 #define bch2_dev_inconsistent_on(cond, ca, ...)                         \
61 ({                                                                      \
62         int _ret = !!(cond);                                            \
63                                                                         \
64         if (_ret)                                                       \
65                 bch2_dev_inconsistent(ca, __VA_ARGS__);                 \
66         _ret;                                                           \
67 })
68
69 /*
70  * Fsck errors: inconsistency errors we detect at mount time, and should ideally
71  * be able to repair:
72  */
73
74 enum {
75         BCH_FSCK_OK                     = 0,
76         BCH_FSCK_ERRORS_NOT_FIXED       = 1,
77         BCH_FSCK_REPAIR_UNIMPLEMENTED   = 2,
78         BCH_FSCK_REPAIR_IMPOSSIBLE      = 3,
79         BCH_FSCK_UNKNOWN_VERSION        = 4,
80 };
81
82 enum fsck_err_opts {
83         FSCK_OPT_EXIT,
84         FSCK_OPT_YES,
85         FSCK_OPT_NO,
86         FSCK_OPT_ASK,
87 };
88
89 enum fsck_err_ret {
90         FSCK_ERR_IGNORE = 0,
91         FSCK_ERR_FIX    = 1,
92         FSCK_ERR_EXIT   = 2,
93         FSCK_ERR_START_TOPOLOGY_REPAIR = 3,
94 };
95
96 struct fsck_err_state {
97         struct list_head        list;
98         const char              *fmt;
99         u64                     nr;
100         bool                    ratelimited;
101         char                    buf[512];
102 };
103
104 #define FSCK_CAN_FIX            (1 << 0)
105 #define FSCK_CAN_IGNORE         (1 << 1)
106 #define FSCK_NEED_FSCK          (1 << 2)
107 #define FSCK_NO_RATELIMIT       (1 << 3)
108
109 __printf(3, 4) __cold
110 enum fsck_err_ret bch2_fsck_err(struct bch_fs *,
111                                 unsigned, const char *, ...);
112 void bch2_flush_fsck_errs(struct bch_fs *);
113
114 #define __fsck_err(c, _flags, msg, ...)                                 \
115 ({                                                                      \
116         int _fix = bch2_fsck_err(c, _flags, msg, ##__VA_ARGS__);\
117                                                                         \
118         if (_fix == FSCK_ERR_EXIT) {                                    \
119                 bch_err(c, "Unable to continue, halting");              \
120                 ret = BCH_FSCK_ERRORS_NOT_FIXED;                        \
121                 goto fsck_err;                                          \
122         }                                                               \
123                                                                         \
124         _fix;                                                           \
125 })
126
127 /* These macros return true if error should be fixed: */
128
129 /* XXX: mark in superblock that filesystem contains errors, if we ignore: */
130
131 #define __fsck_err_on(cond, c, _flags, ...)                             \
132         ((cond) ? __fsck_err(c, _flags, ##__VA_ARGS__) : false)
133
134 #define need_fsck_err_on(cond, c, ...)                                  \
135         __fsck_err_on(cond, c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
136
137 #define need_fsck_err(c, ...)                                           \
138         __fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
139
140 #define mustfix_fsck_err(c, ...)                                        \
141         __fsck_err(c, FSCK_CAN_FIX, ##__VA_ARGS__)
142
143 #define mustfix_fsck_err_on(cond, c, ...)                               \
144         __fsck_err_on(cond, c, FSCK_CAN_FIX, ##__VA_ARGS__)
145
146 #define fsck_err(c, ...)                                                \
147         __fsck_err(c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
148
149 #define fsck_err_on(cond, c, ...)                                       \
150         __fsck_err_on(cond, c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
151
152 /*
153  * Fatal errors: these don't indicate a bug, but we can't continue running in RW
154  * mode - pretty much just due to metadata IO errors:
155  */
156
157 void bch2_fatal_error(struct bch_fs *);
158
159 #define bch2_fs_fatal_error(c, ...)                                     \
160 do {                                                                    \
161         bch_err(c, __VA_ARGS__);                                        \
162         bch2_fatal_error(c);                                            \
163 } while (0)
164
165 #define bch2_fs_fatal_err_on(cond, c, ...)                              \
166 ({                                                                      \
167         int _ret = !!(cond);                                            \
168                                                                         \
169         if (_ret)                                                       \
170                 bch2_fs_fatal_error(c, __VA_ARGS__);                    \
171         _ret;                                                           \
172 })
173
174 /*
175  * IO errors: either recoverable metadata IO (because we have replicas), or data
176  * IO - we need to log it and print out a message, but we don't (necessarily)
177  * want to shut down the fs:
178  */
179
180 void bch2_io_error_work(struct work_struct *);
181
182 /* Does the error handling without logging a message */
183 void bch2_io_error(struct bch_dev *);
184
185 /* Logs message and handles the error: */
186 #define bch2_dev_io_error(ca, fmt, ...)                                 \
187 do {                                                                    \
188         printk_ratelimited(KERN_ERR "bcachefs (%s): " fmt,              \
189                 (ca)->name, ##__VA_ARGS__);                             \
190         bch2_io_error(ca);                                              \
191 } while (0)
192
193 #define bch2_dev_inum_io_error(ca, _inum, _offset, fmt, ...)            \
194 do {                                                                    \
195         printk_ratelimited(KERN_ERR "bcachefs (%s inum %llu offset %llu): " fmt,\
196                 (ca)->name, (_inum), (_offset), ##__VA_ARGS__);         \
197         bch2_io_error(ca);                                              \
198 } while (0)
199
200 #define bch2_dev_io_err_on(cond, ca, ...)                               \
201 ({                                                                      \
202         bool _ret = (cond);                                             \
203                                                                         \
204         if (_ret)                                                       \
205                 bch2_dev_io_error(ca, __VA_ARGS__);                     \
206         _ret;                                                           \
207 })
208
209 #define bch2_dev_inum_io_err_on(cond, ca, _inum, _offset, ...)          \
210 ({                                                                      \
211         bool _ret = (cond);                                             \
212                                                                         \
213         if (_ret)                                                       \
214                 bch2_dev_inum_io_error(ca, _inum, _offset, __VA_ARGS__);\
215         _ret;                                                           \
216 })
217
218 #endif /* _BCACHEFS_ERROR_H */