]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.h
Update bcachefs sources to b1a4dc53be bcachefs: Set lost+found mode to 0700
[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  * 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         bool                    ratelimited;
118         char                    buf[512];
119 };
120
121 #define FSCK_CAN_FIX            (1 << 0)
122 #define FSCK_CAN_IGNORE         (1 << 1)
123 #define FSCK_NEED_FSCK          (1 << 2)
124
125 enum fsck_err_ret bch2_fsck_err(struct bch_fs *,
126                                 unsigned, const char *, ...);
127 void bch2_flush_fsck_errs(struct bch_fs *);
128
129 #define __fsck_err(c, _flags, msg, ...)                                 \
130 ({                                                                      \
131         int _fix = bch2_fsck_err(c, _flags, msg, ##__VA_ARGS__);\
132                                                                         \
133         if (_fix == FSCK_ERR_EXIT) {                                    \
134                 bch_err(c, "Unable to continue, halting");              \
135                 ret = BCH_FSCK_ERRORS_NOT_FIXED;                        \
136                 goto fsck_err;                                          \
137         }                                                               \
138                                                                         \
139         _fix;                                                           \
140 })
141
142 /* These macros return true if error should be fixed: */
143
144 /* XXX: mark in superblock that filesystem contains errors, if we ignore: */
145
146 #define __fsck_err_on(cond, c, _flags, ...)                             \
147         ((cond) ? __fsck_err(c, _flags, ##__VA_ARGS__) : false)
148
149 #define need_fsck_err_on(cond, c, ...)                                  \
150         __fsck_err_on(cond, c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
151
152 #define need_fsck_err(c, ...)                                           \
153         __fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
154
155 #define mustfix_fsck_err(c, ...)                                        \
156         __fsck_err(c, FSCK_CAN_FIX, ##__VA_ARGS__)
157
158 #define mustfix_fsck_err_on(cond, c, ...)                               \
159         __fsck_err_on(cond, c, FSCK_CAN_FIX, ##__VA_ARGS__)
160
161 #define fsck_err(c, ...)                                                \
162         __fsck_err(c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
163
164 #define fsck_err_on(cond, c, ...)                                       \
165         __fsck_err_on(cond, c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
166
167 /*
168  * Fatal errors: these don't indicate a bug, but we can't continue running in RW
169  * mode - pretty much just due to metadata IO errors:
170  */
171
172 void bch2_fatal_error(struct bch_fs *);
173
174 #define bch2_fs_fatal_error(c, ...)                                     \
175 do {                                                                    \
176         bch_err(c, __VA_ARGS__);                                        \
177         bch2_fatal_error(c);                                            \
178 } while (0)
179
180 #define bch2_fs_fatal_err_on(cond, c, ...)                              \
181 ({                                                                      \
182         int _ret = !!(cond);                                            \
183                                                                         \
184         if (_ret)                                                       \
185                 bch2_fs_fatal_error(c, __VA_ARGS__);                    \
186         _ret;                                                           \
187 })
188
189 /*
190  * IO errors: either recoverable metadata IO (because we have replicas), or data
191  * IO - we need to log it and print out a message, but we don't (necessarily)
192  * want to shut down the fs:
193  */
194
195 void bch2_io_error_work(struct work_struct *);
196
197 /* Does the error handling without logging a message */
198 void bch2_io_error(struct bch_dev *);
199
200 /* Logs message and handles the error: */
201 #define bch2_dev_io_error(ca, fmt, ...)                                 \
202 do {                                                                    \
203         printk_ratelimited(KERN_ERR bch2_fmt((ca)->fs,                  \
204                 "IO error on %s for " fmt),                             \
205                 (ca)->name, ##__VA_ARGS__);                             \
206         bch2_io_error(ca);                                              \
207 } while (0)
208
209 #define bch2_dev_io_err_on(cond, ca, ...)                               \
210 ({                                                                      \
211         bool _ret = (cond);                                             \
212                                                                         \
213         if (_ret)                                                       \
214                 bch2_dev_io_error(ca, __VA_ARGS__);                     \
215         _ret;                                                           \
216 })
217
218 /* kill? */
219
220 #define __bcache_io_error(c, fmt, ...)                                  \
221         printk_ratelimited(KERN_ERR bch2_fmt(c,                         \
222                         "IO error: " fmt), ##__VA_ARGS__)
223
224 #define bcache_io_error(c, bio, fmt, ...)                               \
225 do {                                                                    \
226         __bcache_io_error(c, fmt, ##__VA_ARGS__);                       \
227         (bio)->bi_status = BLK_STS_IOERR;                                       \
228 } while (0)
229
230 #endif /* _BCACHEFS_ERROR_H */