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