]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.h
83d3a6274eb72e899ad759d9c6f39b1abc8031c5
[bcachefs-tools-debian] / libbcachefs / error.h
1 #ifndef _BCACHE_ERROR_H
2 #define _BCACHE_ERROR_H
3
4 #include <linux/printk.h>
5
6 struct bch_dev;
7 struct bch_fs;
8
9 /*
10  * XXX: separate out errors that indicate on disk data is inconsistent, and flag
11  * superblock as such
12  */
13
14 /* Error messages: */
15
16 /*
17  * Very fatal logic/inconsistency errors: these indicate that we've majorly
18  * screwed up at runtime, i.e. it's not likely that it was just caused by the
19  * data on disk being inconsistent. These BUG():
20  *
21  * XXX: audit and convert to inconsistent() checks
22  */
23
24 #define bch2_fs_bug(c, ...)                                             \
25 do {                                                                    \
26         bch_err(c, __VA_ARGS__);                                        \
27         BUG();                                                          \
28 } while (0)
29
30 #define bch2_fs_bug_on(cond, c, ...)                                    \
31 do {                                                                    \
32         if (cond)                                                       \
33                 bch2_fs_bug(c, __VA_ARGS__);                            \
34 } while (0)
35
36 /*
37  * Inconsistency errors: The on disk data is inconsistent. If these occur during
38  * initial recovery, they don't indicate a bug in the running code - we walk all
39  * the metadata before modifying anything. If they occur at runtime, they
40  * indicate either a bug in the running code or (less likely) data is being
41  * silently corrupted under us.
42  *
43  * XXX: audit all inconsistent errors and make sure they're all recoverable, in
44  * BCH_ON_ERROR_CONTINUE mode
45  */
46
47 void bch2_inconsistent_error(struct bch_fs *);
48
49 #define bch2_fs_inconsistent(c, ...)                                    \
50 do {                                                                    \
51         bch_err(c, __VA_ARGS__);                                        \
52         bch2_inconsistent_error(c);                                     \
53 } while (0)
54
55 #define bch2_fs_inconsistent_on(cond, c, ...)                           \
56 ({                                                                      \
57         int _ret = !!(cond);                                            \
58                                                                         \
59         if (_ret)                                                       \
60                 bch2_fs_inconsistent(c, __VA_ARGS__);                   \
61         _ret;                                                           \
62 })
63
64 /*
65  * Later we might want to mark only the particular device inconsistent, not the
66  * entire filesystem:
67  */
68
69 #define bch2_dev_inconsistent(ca, ...)                                  \
70 do {                                                                    \
71         bch_err(ca, __VA_ARGS__);                                       \
72         bch2_inconsistent_error((ca)->fs);                              \
73 } while (0)
74
75 #define bch2_dev_inconsistent_on(cond, ca, ...)                         \
76 ({                                                                      \
77         int _ret = !!(cond);                                            \
78                                                                         \
79         if (_ret)                                                       \
80                 bch2_dev_inconsistent(ca, __VA_ARGS__);                 \
81         _ret;                                                           \
82 })
83
84 /*
85  * Fsck errors: inconsistency errors we detect at mount time, and should ideally
86  * be able to repair:
87  */
88
89 enum {
90         BCH_FSCK_OK                     = 0,
91         BCH_FSCK_ERRORS_NOT_FIXED       = 1,
92         BCH_FSCK_REPAIR_UNIMPLEMENTED   = 2,
93         BCH_FSCK_REPAIR_IMPOSSIBLE      = 3,
94         BCH_FSCK_UNKNOWN_VERSION        = 4,
95 };
96
97 /* These macros return true if error should be fixed: */
98
99 /* XXX: mark in superblock that filesystem contains errors, if we ignore: */
100
101 #ifndef __fsck_err
102 #define __fsck_err(c, _can_fix, _can_ignore, _nofix_msg, msg, ...)      \
103 ({                                                                      \
104         bool _fix = false;                                              \
105                                                                         \
106         if (_can_fix && (c)->opts.fix_errors) {                         \
107                 bch_err(c, msg ", fixing", ##__VA_ARGS__);              \
108                 set_bit(BCH_FS_FSCK_FIXED_ERRORS, &(c)->flags); \
109                 _fix = true;                                            \
110         } else if (_can_ignore &&                                       \
111                    (c)->opts.errors == BCH_ON_ERROR_CONTINUE) {         \
112                 bch_err(c, msg " (ignoring)", ##__VA_ARGS__);           \
113         } else {                                                        \
114                 bch_err(c, msg " ("_nofix_msg")", ##__VA_ARGS__);       \
115                 ret = BCH_FSCK_ERRORS_NOT_FIXED;                        \
116                 goto fsck_err;                                          \
117         }                                                               \
118                                                                         \
119         BUG_ON(!_fix && !_can_ignore);                                  \
120         _fix;                                                           \
121 })
122 #endif
123
124 #define __fsck_err_on(cond, c, _can_fix, _can_ignore, _nofix_msg, ...)  \
125         ((cond) ? __fsck_err(c, _can_fix, _can_ignore,                  \
126                              _nofix_msg, ##__VA_ARGS__) : false)
127
128 #define unfixable_fsck_err_on(cond, c, ...)                             \
129         __fsck_err_on(cond, c, false, true, "repair unimplemented", ##__VA_ARGS__)
130
131 #define need_fsck_err_on(cond, c, ...)                                  \
132         __fsck_err_on(cond, c, false, true, "run fsck to correct", ##__VA_ARGS__)
133
134 #define mustfix_fsck_err(c, ...)                                        \
135         __fsck_err(c, true, false, "not fixing", ##__VA_ARGS__)
136
137 #define mustfix_fsck_err_on(cond, c, ...)                               \
138         __fsck_err_on(cond, c, true, false, "not fixing", ##__VA_ARGS__)
139
140 #define fsck_err_on(cond, c, ...)                                       \
141         __fsck_err_on(cond, c, true, true, "not fixing", ##__VA_ARGS__)
142
143 /*
144  * Fatal errors: these don't indicate a bug, but we can't continue running in RW
145  * mode - pretty much just due to metadata IO errors:
146  */
147
148 void bch2_fatal_error(struct bch_fs *);
149
150 #define bch2_fs_fatal_error(c, ...)                                     \
151 do {                                                                    \
152         bch_err(c, __VA_ARGS__);                                        \
153         bch2_fatal_error(c);                                            \
154 } while (0)
155
156 #define bch2_fs_fatal_err_on(cond, c, ...)                              \
157 ({                                                                      \
158         int _ret = !!(cond);                                            \
159                                                                         \
160         if (_ret)                                                       \
161                 bch2_fs_fatal_error(c, __VA_ARGS__);                    \
162         _ret;                                                           \
163 })
164
165 #define bch2_dev_fatal_error(ca, ...)                                   \
166 do {                                                                    \
167         bch_err(ca, __VA_ARGS__);                                       \
168         bch2_fatal_error(c);                                            \
169 } while (0)
170
171 #define bch2_dev_fatal_io_error(ca, fmt, ...)                           \
172 do {                                                                    \
173         printk_ratelimited(KERN_ERR bch2_fmt((ca)->fs,                  \
174                 "fatal IO error on %s for " fmt),                       \
175                 (ca)->name, ##__VA_ARGS__);                             \
176         bch2_fatal_error((ca)->fs);                                     \
177 } while (0)
178
179 #define bch2_dev_fatal_io_err_on(cond, ca, ...)                         \
180 ({                                                                      \
181         int _ret = !!(cond);                                            \
182                                                                         \
183         if (_ret)                                                       \
184                 bch2_dev_fatal_io_error(ca, __VA_ARGS__);               \
185         _ret;                                                           \
186 })
187
188 /*
189  * Nonfatal IO errors: either recoverable metadata IO (because we have
190  * replicas), or data IO - we need to log it and print out a message, but we
191  * don't (necessarily) want to shut down the fs:
192  */
193
194 void bch2_nonfatal_io_error_work(struct work_struct *);
195
196 /* Does the error handling without logging a message */
197 void bch2_nonfatal_io_error(struct bch_dev *);
198
199 #if 0
200 #define bch2_fs_nonfatal_io_error(c, ...)                               \
201 do {                                                                    \
202         bch_err(c, __VA_ARGS__);                                        \
203         bch2_nonfatal_io_error(c);                                      \
204 } while (0)
205 #endif
206
207 /* Logs message and handles the error: */
208 #define bch2_dev_nonfatal_io_error(ca, fmt, ...)                                \
209 do {                                                                    \
210         printk_ratelimited(KERN_ERR bch2_fmt((ca)->fs,                  \
211                 "IO error on %s for " fmt),                             \
212                 (ca)->name, ##__VA_ARGS__);                             \
213         bch2_nonfatal_io_error(ca);                                     \
214 } while (0)
215
216 #define bch2_dev_nonfatal_io_err_on(cond, ca, ...)                      \
217 ({                                                                      \
218         bool _ret = (cond);                                             \
219                                                                         \
220         if (_ret)                                                       \
221                 bch2_dev_nonfatal_io_error(ca, __VA_ARGS__);            \
222         _ret;                                                           \
223 })
224
225 /* kill? */
226
227 #define __bcache_io_error(c, fmt, ...)                                  \
228         printk_ratelimited(KERN_ERR bch2_fmt(c,                         \
229                         "IO error: " fmt), ##__VA_ARGS__)
230
231 #define bcache_io_error(c, bio, fmt, ...)                               \
232 do {                                                                    \
233         __bcache_io_error(c, fmt, ##__VA_ARGS__);                       \
234         (bio)->bi_error = -EIO;                                         \
235 } while (0)
236
237 #endif /* _BCACHE_ERROR_H */