]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.h
bbf9b6d85b4dcf55d794afd88ed52d228e40eba8
[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         bool _ret = unlikely(!!(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         bool _ret = unlikely(!!(cond));                                 \
63                                                                         \
64         if (_ret)                                                       \
65                 bch2_dev_inconsistent(ca, __VA_ARGS__);                 \
66         _ret;                                                           \
67 })
68
69 /*
70  * When a transaction update discovers or is causing a fs inconsistency, it's
71  * helpful to also dump the pending updates:
72  */
73 #define bch2_trans_inconsistent(trans, ...)                             \
74 ({                                                                      \
75         bch_err(trans->c, __VA_ARGS__);                                 \
76         bch2_inconsistent_error(trans->c);                              \
77         bch2_dump_trans_updates(trans);                                 \
78 })
79
80 #define bch2_trans_inconsistent_on(cond, trans, ...)                    \
81 ({                                                                      \
82         bool _ret = unlikely(!!(cond));                                 \
83                                                                         \
84         if (_ret)                                                       \
85                 bch2_trans_inconsistent(trans, __VA_ARGS__);            \
86         _ret;                                                           \
87 })
88
89 /*
90  * Fsck errors: inconsistency errors we detect at mount time, and should ideally
91  * be able to repair:
92  */
93
94 enum fsck_err_opts {
95         FSCK_OPT_EXIT,
96         FSCK_OPT_YES,
97         FSCK_OPT_NO,
98         FSCK_OPT_ASK,
99 };
100
101 struct fsck_err_state {
102         struct list_head        list;
103         const char              *fmt;
104         u64                     nr;
105         bool                    ratelimited;
106         struct printbuf         buf;
107 };
108
109 #define FSCK_CAN_FIX            (1 << 0)
110 #define FSCK_CAN_IGNORE         (1 << 1)
111 #define FSCK_NEED_FSCK          (1 << 2)
112 #define FSCK_NO_RATELIMIT       (1 << 3)
113
114 __printf(3, 4) __cold
115 int bch2_fsck_err(struct bch_fs *, unsigned, const char *, ...);
116 void bch2_flush_fsck_errs(struct bch_fs *);
117
118 #define __fsck_err(c, _flags, msg, ...)                                 \
119 ({                                                                      \
120         int _ret = bch2_fsck_err(c, _flags, msg, ##__VA_ARGS__);        \
121                                                                         \
122         if (_ret != -BCH_ERR_fsck_fix &&                                \
123             _ret != -BCH_ERR_fsck_ignore) {                             \
124                 ret = _ret;                                             \
125                 goto fsck_err;                                          \
126         }                                                               \
127                                                                         \
128         _ret == -BCH_ERR_fsck_fix;                                      \
129 })
130
131 /* These macros return true if error should be fixed: */
132
133 /* XXX: mark in superblock that filesystem contains errors, if we ignore: */
134
135 #define __fsck_err_on(cond, c, _flags, ...)                             \
136         (unlikely(cond) ? __fsck_err(c, _flags, ##__VA_ARGS__) : false)
137
138 #define need_fsck_err_on(cond, c, ...)                                  \
139         __fsck_err_on(cond, c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
140
141 #define need_fsck_err(c, ...)                                           \
142         __fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
143
144 #define mustfix_fsck_err(c, ...)                                        \
145         __fsck_err(c, FSCK_CAN_FIX, ##__VA_ARGS__)
146
147 #define mustfix_fsck_err_on(cond, c, ...)                               \
148         __fsck_err_on(cond, c, FSCK_CAN_FIX, ##__VA_ARGS__)
149
150 #define fsck_err(c, ...)                                                \
151         __fsck_err(c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
152
153 #define fsck_err_on(cond, c, ...)                                       \
154         __fsck_err_on(cond, c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
155
156 /*
157  * Fatal errors: these don't indicate a bug, but we can't continue running in RW
158  * mode - pretty much just due to metadata IO errors:
159  */
160
161 void bch2_fatal_error(struct bch_fs *);
162
163 #define bch2_fs_fatal_error(c, ...)                                     \
164 do {                                                                    \
165         bch_err(c, __VA_ARGS__);                                        \
166         bch2_fatal_error(c);                                            \
167 } while (0)
168
169 #define bch2_fs_fatal_err_on(cond, c, ...)                              \
170 ({                                                                      \
171         bool _ret = unlikely(!!(cond));                                 \
172                                                                         \
173         if (_ret)                                                       \
174                 bch2_fs_fatal_error(c, __VA_ARGS__);                    \
175         _ret;                                                           \
176 })
177
178 /*
179  * IO errors: either recoverable metadata IO (because we have replicas), or data
180  * IO - we need to log it and print out a message, but we don't (necessarily)
181  * want to shut down the fs:
182  */
183
184 void bch2_io_error_work(struct work_struct *);
185
186 /* Does the error handling without logging a message */
187 void bch2_io_error(struct bch_dev *);
188
189 /* Logs message and handles the error: */
190 #define bch2_dev_io_error(ca, fmt, ...)                                 \
191 do {                                                                    \
192         printk_ratelimited(KERN_ERR "bcachefs (%s): " fmt,              \
193                 (ca)->name, ##__VA_ARGS__);                             \
194         bch2_io_error(ca);                                              \
195 } while (0)
196
197 #define bch2_dev_inum_io_error(ca, _inum, _offset, fmt, ...)            \
198 do {                                                                    \
199         printk_ratelimited(KERN_ERR "bcachefs (%s inum %llu offset %llu): " fmt,\
200                 (ca)->name, (_inum), (_offset), ##__VA_ARGS__);         \
201         bch2_io_error(ca);                                              \
202 } while (0)
203
204 #define bch2_dev_io_err_on(cond, ca, ...)                               \
205 ({                                                                      \
206         bool _ret = (cond);                                             \
207                                                                         \
208         if (_ret)                                                       \
209                 bch2_dev_io_error(ca, __VA_ARGS__);                     \
210         _ret;                                                           \
211 })
212
213 #define bch2_dev_inum_io_err_on(cond, ca, _inum, _offset, ...)          \
214 ({                                                                      \
215         bool _ret = (cond);                                             \
216                                                                         \
217         if (_ret)                                                       \
218                 bch2_dev_inum_io_error(ca, _inum, _offset, __VA_ARGS__);\
219         _ret;                                                           \
220 })
221
222 #endif /* _BCACHEFS_ERROR_H */