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